本文介绍了我无法验证radiobutton列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要检查至少一个radiobutton。我的asp代码看起来像
< asp:RadioButtonList ID = rbtOdgovor runat = server RepeatLayout = 表 AutoPostBack = True CausesValidation = True > < asp: ListItem 文字 = 是 值 = rbtYes > < / asp:ListItem > < asp:ListItem 文本 = 否 值 = rbtNo > < / asp:ListItem > < / asp:RadioButtonList > < asp:RequiredFieldValidator ID = ValidatorOdgovor runat = server ControlToValidate = rbtOdgovor ErrorMessage = 请输入一个值; ValidateRequestMode = 已启用 > < ; / asp:RequiredFieldValidator >< asp:按钮 runat = 服务器 ID = btnSave 文字 = 答案 字体名称 = Tahoma 高度 = 30px 宽度 = 100px OnClick = btnSave_Click ValidateRequestMode = 已启用 / >
但它不起作用!!
解决方案查看此 forums.asp/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 >protected void CustomValidator1_ServerValidate( object source,ServerValidateEventArgs args) { args.IsValid = RadioButton1.Checked || RadioButton2.Checked; } 受保护 void Button1_Click( object sender,EventArgs e) { if (Page.IsValid) { // 验证成功。 } }
代码块已更正
< 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 runat = server ControlToValidate = RadioButtonList1 ErrorMessage = 您必须选择自己喜欢的颜色! > * < / 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 forums.asp/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列表
发布评论