我无法验证radiobutton列表 [英] I can't validate a radiobutton list
问题描述
我想要检查至少一个radiobutton。我的asp代码看起来像
< asp:RadioButtonList ID = rbtOdgovor runat = server
< span class =code-attribute> RepeatLayout = 表 AutoPostBack = True CausesValidation = True >
< asp: ListItem 文字 = 是 值 = rbtYes > < / asp:ListItem >
< asp:ListItem 文本 = 否 值 = rbtNo > < / asp:ListItem >
< / asp:RadioButtonList >
< asp:RequiredFieldValidator
ID < span class =code-keyword> = ValidatorOdgovor
< span class =code-attribute>
< span class =code-attribute> runat = server
< span class =code-attribute> ControlToValidate = rbtOdgovor
ErrorMessage = 请输入一个值; ValidateRequestMode = 已启用 > < ; / asp:RequiredFieldValidator >
< asp:按钮 runat = 服务器 ID = btnSave 文字 = 答案 字体名称 = Tahoma 高度 = 30px 宽度 = 100px OnClick = btnSave_Click ValidateRequestMode = 已启用 / >
但它不起作用!!
查看此
http://forums.asp.net/t/1199803.aspx [ ^ ]
最简单的方法如上来自Jos的方法。使用RadioButtonList和RequiredFieldValidator。
< asp:RadioButtonList ID = RadioButtonList1 runat = 服务器 >
< asp:ListItem > north < / asp:ListItem >
< asp:ListItem > west < / asp:ListItem >
< / asp:RadioButtonList >
< ; asp:RequiredFieldValidator ID = RequiredFieldValidator1 runat = server
ControlToValidate = RadioButtonList1 ErrorMessage = RequiredFieldValidator >
< / asp:RequiredFieldValidator >
2.机智hout RadioButtonList,如果你想要一组radiobutton,你也可以通过CustomValidator实现它。
< script language = javascript type = text / javascript >
函数CustomValidator1_ClientValidate(source,args)
{
if (document.getElementById( <%= RadioButton1.ClientID%>)。 checked || document.getElementById( <%= RadioButton2.ClientID%>)。已检查)
{
args.IsValid = true ;
}
else
{
args.IsValid = false 跨度>;
}
}
// - >
< / script >
< body>
< form id = form1 runat = server >
< div>
< asp:RadioButton ID = RadioButton1 runat = server GroupName = location Text = north />
< asp:RadioButton ID = RadioButton2 runat = server GroupName = location Text = west />
< asp:按钮ID = Button1 runat = server Text = 按钮 onclick = Button1_Click />
< asp:CustomValidator id = CustomValidator1 runat = server Display = 动态 ErrorMessage = 请选择 ClientValidationFunction = CustomValidator1_ClientValidate OnServerValidate = CustomValidator1_ServerValidate > < / asp:CustomValidator >
< / div >
< / 表格 >
< / body >
< pre lang =c#> protected void CustomValidator1_ServerValidate( object source,ServerValidateEventArgs args)
{
args.IsValid = RadioButton1.Checked || RadioButton2.Checked;
}
受保护 void Button1_Click( object sender,EventArgs e)
{
if (Page.IsValid)
{
< span class =code-comment> // 验证成功。
}
}
代码块已更正
< asp:RadioButtonList
ID = RadioButtonList1
runat = server
RepeatColumns = 3 >
< asp:ListItem > 红色< / asp:ListItem >
< asp:ListItem > 黄色< / asp:ListItem >
< asp:ListItem > 蓝色< / asp:ListItem >
< asp:ListItem > 绿色< / asp:ListItem >
< / asp:RadioButtonList >
< asp:RequiredFieldValidator
ID = ReqiredFieldValidator1 < span class =code-attribute>
< span class =code-attribute> runat = server
ControlToValidate = RadioButtonList1
ErrorMessage < span class =code-keyword> = 您必须选择自己喜欢的颜色! > *
< / asp:RequiredFieldValidator > 跨度>
I want at least one radiobutton to be checked. My asp code looks like
<asp:RadioButtonList ID="rbtOdgovor" runat="server"
RepeatLayout="Table" AutoPostBack="True" CausesValidation="True">
<asp:ListItem Text="yes" Value="rbtYes"></asp:ListItem>
<asp:ListItem Text="No" Value="rbtNo"></asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator
ID="ValidatorOdgovor"
runat="server"
ControlToValidate="rbtOdgovor"
ErrorMessage="Please enter a value; ValidateRequestMode="Enabled"></asp:RequiredFieldValidator>
<asp:Button runat="server" ID="btnSave" Text="Answer" Font-Names="Tahoma" Height="30px" Width="100px" OnClick="btnSave_Click" ValidateRequestMode="Enabled" />
But it doesnt work!!
Hi,
Check this
http://forums.asp.net/t/1199803.aspx[^]
Hi,
The simplest way is as the above approach from Jos. Using a RadioButtonList and a RequiredFieldValidator.
<asp:RadioButtonList ID="RadioButtonList1" runat="server"> <asp:ListItem>north</asp:ListItem> <asp:ListItem>west</asp:ListItem> </asp:RadioButtonList> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="RadioButtonList1" ErrorMessage="RequiredFieldValidator"> </asp:RequiredFieldValidator>
2. Without RadioButtonList and if you want a group radiobutton insteads, you can also implement it by CustomValidator.
<script language="javascript" type="text/javascript" > function CustomValidator1_ClientValidate(source,args) { if(document.getElementById("<%= RadioButton1.ClientID %>").checked || document.getElementById("<%= RadioButton2.ClientID %>").checked) { args.IsValid = true; } else { args.IsValid = false; } } //--> </script> <body> <form id="form1" runat="server"> <div> <asp:RadioButton ID="RadioButton1" runat="server" GroupName="location" Text="north" /> <asp:RadioButton ID="RadioButton2" runat="server" GroupName="location" Text="west" /> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <asp:CustomValidator id="CustomValidator1" runat="server" Display="Dynamic" ErrorMessage="please choose" ClientValidationFunction="CustomValidator1_ClientValidate" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator> </div> </form> </body>
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { args.IsValid = RadioButton1.Checked || RadioButton2.Checked; } protected void Button1_Click(object sender, EventArgs e) { if (Page.IsValid) { //validate is successful. } }
code blocks corrected
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatColumns="3"> <asp:ListItem>Red</asp:ListItem> <asp:ListItem>Yellow</asp:ListItem> <asp:ListItem>Blue</asp:ListItem> <asp:ListItem>Green</asp:ListItem> </asp:RadioButtonList> <asp:RequiredFieldValidator ID="ReqiredFieldValidator1" runat="server" ControlToValidate="RadioButtonList1" ErrorMessage="You must Select your favorite color!">* </asp:RequiredFieldValidator>
这篇关于我无法验证radiobutton列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!