我需要Cutom验证每个在C#中选择了一个radiobutton的radioButtonList请... [英] I need to Cutom Validate each radioButtonList that has one radiobutton selected in C# please..
本文介绍了我需要Cutom验证每个在C#中选择了一个radiobutton的radioButtonList请...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C#中我需要自定义验证每个radioButtonList都选择了一个radiobutton(注意这里:RadioButtonList和CustomValidator都在另一个DataList中重复)
解决方案
尝试这个
-----------
< body >
< 表格 id = form1 runat = 服务器 >
< div >
< asp:DataList ID = DataList1 < span class =code-attribute> runat = server DataKeyField = au_id >
< ItemTemplate >
< br / >
au_lna我:
< asp:标签 ID = au_lnameLabel runat = server 文字 =' <% #Eval( au_lname)%> ' / >
< ; br/ >
< asp:RadioButtonList ID = rclCollection runat = 服务器 > ;
< asp:ListItem 文字 = 一个 值 = 0 >< / asp:ListItem >
< asp:ListItem 文字 = 两个 值 = 1 > < / asp:ListItem >
< / asp:RadioButtonList >
< br / >
< ; br / < span class =code-keyword>>
< / ItemTemplate >
< / asp: DataList >
< asp:CustomValidator ID = CustomValidator1 runat = serv呃 ErrorMessage = 选择任何选项
OnServerValidate = CustomValidator1_ServerValidate > < / asp:CustomValidator >
< asp:Button ID = < span class =code-keyword> btnSave runat = server 文字 = 保存 / >
< / div >
< / form >
< / body >
< / html >
--- --------------------
服务器端
-------------------- ---
< pre lang = cs < span class =code-keyword>> protected void CustomValidator1_ServerValidate(object source,ServerValidateEventArgs args)
{
for(int i = 0; i & lt; DataList1.Items.Count; i ++)
{
int cnt = 0;
RadioButtonList rclCollection =(RadioButtonList)DataList1.Items [i] .FindControl(& quot; rclCollection& quot;);
for(int j = 0; j & lt; rclCollection.Items.Count; j ++)
{
if(rclCollection .Items [j] .Selected == false)
cnt ++;
}
if(cnt & lt; rclCollection.Items.Count)
{
args.IsValid = false ;
返回;
}
}
args.IsValid = true;
} < / pre > < /跨度>
in C# I need to custom validate that each radioButtonList has one radiobutton selected (note here : both RadioButtonList and the CustomValidator are repeated in another DataList)
解决方案
Try this
-----------
<body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server" DataKeyField="au_id"> <ItemTemplate> <br /> au_lname: <asp:Label ID="au_lnameLabel" runat="server" Text='<%# Eval("au_lname") %>' /> <br /> <asp:RadioButtonList ID="rclCollection" runat="server"> <asp:ListItem Text="One" Value="0"></asp:ListItem> <asp:ListItem Text="Two" Value="1"></asp:ListItem> </asp:RadioButtonList> <br /> <br /> </ItemTemplate> </asp:DataList> <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Select any option" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator> <asp:Button ID="btnSave" runat="server" Text="save" /> </div> </form> </body> </html> ----------------------- Server Side ----------------------- <pre lang="cs">protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { for (int i = 0; i < DataList1.Items.Count; i++) { int cnt=0; RadioButtonList rclCollection = (RadioButtonList)DataList1.Items[i].FindControl("rclCollection"); for (int j = 0; j < rclCollection.Items.Count; j++) { if (rclCollection.Items[j].Selected == false) cnt++; } if (cnt < rclCollection.Items.Count) { args.IsValid = false; return; } } args.IsValid = true; }</pre>
这篇关于我需要Cutom验证每个在C#中选择了一个radiobutton的radioButtonList请...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文