我有多个验证总结来验证我的单选按钮。如何只使用1个按钮来调用我的验证? [英] I have multiple validationsummary to validate my radio buttons. How can I use just 1 button to call my validation?

查看:62
本文介绍了我有多个验证总结来验证我的单选按钮。如何只使用1个按钮来调用我的验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个ValidationSummary来验证我的单选按钮。如何只用一个按钮来调用我的验证?



我不能在我的网站上使用RadioButtonList。但我需要验证我的单选按钮。我也不能默认选中我的一个单选按钮。



所以我的困境是弄清楚如何只用一个按钮验证我的单选按钮组。我正在使用ASP.NET和C#



提前谢谢



我有什么试过:



< asp:ValidationSummary ID =ValidationSummary1ValidationGroup =sample1DisplayMode =Listrunat =serverForeColor =红色Font-Bold =false/>

< asp:RadioButton ID =RadioBtn1runat =serverValidationGroup =sample1GroupName =rpt1Text =One />

< asp:RadioButton ID =RadioBtn2runat =serverValidationGroup =sample1GroupName =rpt1Text =Two/>



< asp:ValidationSummary ID =ValidationSummary2ValidationGroup =sample2DisplayMode =Listrunat =serverForeColor =RedFont-Bold =false/>

< asp:RadioButton ID =RadioBtn3runat =serverValidationGroup =sample2GroupName =rpt2Text =Three/>

< ; asp:RadioButton ID =RadioBtn4runat =serverValidationGroup =sample2Group Name =rpt2Text =Four/>







< asp:CustomValidator ID =CustomValidator1runat =serverValidationGroup =sample1

ErrorMessage =请选择其中一个单选按钮

OnServerValidate =ValidateSample/>



< asp:CustomValidator ID =CustomValidator2runat =serverValidationGroup =sample2

ErrorMessage =请选择一个单选按钮

OnServerValidate =ValidateSample/>



< asp:按钮ID =Button1runat =服务器ValidationGroup =sample1Text =Submit/>

< asp:按钮ID =Button2runat =serverValidationGroup =sample2Text =Submit/> ;







protected void ValidateSample(对象源,ServerValidateEventArgs ar gs)

{

if((RadioBtn1.Checked)|| (RadioBtn2.Checked)|| (RadioBtn3.Checked)|| (RadioBtn4.Checked))

{

args.IsValid = true;

}

else

{

args.IsValid = false;

}

}

解决方案

验证组旨在为不同的按钮提供不同的验证集。如果您希望在单击一个按钮时触发所有验证器,则要么不设置验证组,要么将它们全部设置为相同的值。



验证摘要用于显示表单上所有验证错误的摘要。它并不意味着显示单个控件的验证错误。使用验证器控件来执行此操作。

 <   asp:CustomValidator     runat   =  server    ForeColor   = 红色   字体粗体  =  false   显示  = 动态   文字  = 请选择其中一个单选按钮    OnServerValidate   =  ValidateSample1     /  >  
< asp:RadioButton ID = RadioBtn1 runat = server ValidationGroup = sample1 GroupName = rpt1 文字 = 一个 / >
< asp:RadioButton ID = RadioBtn2 runat = server ValidationGroup = sample1 GroupName = rpt1 文本 = 两个 / >

< asp:CustomValidator runat = server ForeColor = 红色 字体粗体 = false 显示 = 动态 文字 = 请选择其中一个单选按钮 OnServerValidate = ValidateSample2 / >
< asp:RadioButton ID = < span class =code-keyword> RadioBtn3 runat = server ValidationGroup = sample2 GroupName = rpt2 文字 = / >
< asp:RadioButton ID = RadioBtn4 runat = server ValidationGroup = sample2 GroupName = rpt2 文字 < span class =code-keyword> = / >

  protected   void  ValidateSample1( object  sender,ServerValidateEventArgs e)
{
args.IsValid = RadioBtn1.Checked || RadioBtn2.Checked;
}

受保护 void ValidateSample2( object sender,ServerValidateEventArgs e)
{
args.IsValid = RadioBtn3.Checked || RadioBtn4.Checked;
}



现在,帮自己一个忙,给控件带来有意义的ID,而不是接受Visual Studio提供的默认值。您可能还记得 RadioBtn42 意味着现在,但是当你在六个月后回到你的代码时,你会诅咒写作的人它<!/ BLOCKQUOTE>

I have multiple ValidationSummary to validate my radio buttons. How can I use just 1 button to call my Validation?

I can NOT use RadioButtonList on my site. But I need to validate my radio buttons. I also can NOT have 1 of my radio buttons checked by default.

So my dilemma is to figure out how to validate my groups of radio buttons with only 1 button. I'm using ASP.NET and C#

Thanks in advance

What I have tried:

<asp:ValidationSummary ID="ValidationSummary1" ValidationGroup="sample1" DisplayMode="List" runat="server" ForeColor="Red" Font-Bold="false" />
<asp:RadioButton ID="RadioBtn1" runat="server" ValidationGroup="sample1" GroupName="rpt1" Text="One" />
<asp:RadioButton ID="RadioBtn2" runat="server" ValidationGroup="sample1" GroupName="rpt1" Text="Two" />

<asp:ValidationSummary ID="ValidationSummary2" ValidationGroup="sample2" DisplayMode="List" runat="server" ForeColor="Red" Font-Bold="false" />
<asp:RadioButton ID="RadioBtn3" runat="server" ValidationGroup="sample2" GroupName="rpt2" Text="Three" />
<asp:RadioButton ID="RadioBtn4" runat="server" ValidationGroup="sample2" GroupName="rpt2" Text="Four" />



<asp:CustomValidator ID="CustomValidator1" runat="server" ValidationGroup="sample1"
ErrorMessage="Please select one of the radio button"
OnServerValidate="ValidateSample" />

<asp:CustomValidator ID="CustomValidator2" runat="server" ValidationGroup="sample2"
ErrorMessage="Please select one of the radio button"
OnServerValidate="ValidateSample" />

<asp:Button ID="Button1" runat="server" ValidationGroup="sample1" Text="Submit" />
<asp:Button ID="Button2" runat="server" ValidationGroup="sample2" Text="Submit" />



protected void ValidateSample(object source, ServerValidateEventArgs args)
{
if ((RadioBtn1.Checked) || (RadioBtn2.Checked) || (RadioBtn3.Checked) || (RadioBtn4.Checked))
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}

解决方案

Validation groups are intended to provide different sets of validation for different buttons. If you want all of the validators to fire when you click one button, then either don't set the validation groups, or set them all to the same value.

The validation summary is used to display a summary of all validation error on the form. It is not meant to display the validation errors for a single control. Use the validator control to do that.

<asp:CustomValidator runat="server" ForeColor="Red" Font-Bold="false" Display="Dynamic" Text="Please select one of the radio buttons" OnServerValidate="ValidateSample1" />
<asp:RadioButton ID="RadioBtn1" runat="server" ValidationGroup="sample1" GroupName="rpt1" Text="One" />
<asp:RadioButton ID="RadioBtn2" runat="server" ValidationGroup="sample1" GroupName="rpt1" Text="Two" />

<asp:CustomValidator runat="server" ForeColor="Red" Font-Bold="false" Display="Dynamic" Text="Please select one of the radio buttons" OnServerValidate="ValidateSample2" />
<asp:RadioButton ID="RadioBtn3" runat="server" ValidationGroup="sample2" GroupName="rpt2" Text="Three" />
<asp:RadioButton ID="RadioBtn4" runat="server" ValidationGroup="sample2" GroupName="rpt2" Text="Four" />

protected void ValidateSample1(object sender, ServerValidateEventArgs e)
{
    args.IsValid = RadioBtn1.Checked || RadioBtn2.Checked;
}

protected void ValidateSample2(object sender, ServerValidateEventArgs e)
{
    args.IsValid = RadioBtn3.Checked || RadioBtn4.Checked;
}


Now, do yourself a favour and give your controls meaningful IDs, rather than accepting the defaults provided by Visual Studio. You might remember what RadioBtn42 means now, but when you come back to your code in six months time, you'll be cursing the person who wrote it!


这篇关于我有多个验证总结来验证我的单选按钮。如何只使用1个按钮来调用我的验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆