如何在C#中验证ASP.NET中继器中的多个单选按钮 [英] How do I validate multiple radio buttons in a ASP.NET repeater in C#

查看:177
本文介绍了如何在C#中验证ASP.NET中继器中的多个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个单选按钮,我使用组名选择2个选项中的至少一个.我似乎无法获取GroupName,因此可以通过单击提交"按钮来验证它们.

感谢您的帮助

我尝试过的事情:

< asp:CustomValidator ID ="CustomValidator1" runat ="server" ErrorMessage ="*选择一个选项" ForeColor =#ff0000" OnServerValidate ="option1_Validation" Display ="Dynamic"/>

< myRepeater>

< asp:RadioButton ID ="rdOption1" Text ="Option_1" GroupName ="gnOption1" runat ="server"/>

< asp:RadioButton ID ="rdOption2"文本="Option_2" GroupName ="gnOption1" runat =服务器"/>

</myRepeater>

 受保护的void option1_Validation(对象源,ServerValidateEventArgs args)
    {
        bool itemSelected = false;
        foreach(myRepeater.Items中的RepeaterItem ri)
        {
            RadioButton rb =(RadioButton)ri.FindControl("gnOption1");
            {
              如果(rb.GroupName =="gnOption1"&& rb.Checked == true)
                {
                    itemSelected = true;

                }
                args.IsValid = itemSelected;
            }
        }
    } 

解决方案

报价:

RadioButton rb= (RadioButton)ri.FindControl("Game_1");


FindControl方法 [ bool itemSelected = false ; foreach (RepeaterItem ri in myRepeater.Items中) { RadioButton rb =(RadioButton)ri.FindControl(" ); 如果(已选中) { itemSelected = true ; break ; } } args.IsValid = itemSelected;


注意:这将验证是否在任何项目中选择了"Option_1".如果这不是您要验证的内容,则需要解释您的要求.

如果要验证是否已在每个项目中选择了一个单选按钮,请执行以下操作:

 args.IsValid =  true ;

 foreach (RepeaterItem ri  in  myRepeater.Items中)
{
    RadioButton rb =(RadioButton)ri.FindControl(" );
    如果(已选中)继续;
    
    rb =(RadioButton)ri.FindControl(" );
    如果(已选中)继续// 均未选择以下选项:
    args.IsValid =  false ;
     break ;
} 


I have multiple radio buttons and I''m using a group name to choose at least 1 of the 2 options. I can''t seem to get the GroupName so I can validate them with a submit button is clicked.

Thanks for any help

What I have tried:

<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="* Select an option" ForeColor="#ff0000" OnServerValidate="option1_Validation" Display="Dynamic" /> 

<myRepeater>

<asp:RadioButton ID="rdOption1" Text="Option_1" GroupName="gnOption1" runat="server" />

<asp:RadioButton ID="rdOption2" Text="Option_2" GroupName="gnOption1" runat="server" />

</myRepeater>

 protected void option1_Validation(object source, ServerValidateEventArgs args)
    {
        bool itemSelected = false;
        foreach (RepeaterItem ri in myRepeater.Items)
        {
            RadioButton rb= (RadioButton)ri.FindControl("gnOption1");
            {               
              if (rb.GroupName == "gnOption1" && rb.Checked == true)
                {
                    itemSelected = true;

                }
                args.IsValid = itemSelected;
            }
        }
    }

RadioButton rb= (RadioButton)ri.FindControl("Game_1");


The FindControl method[^] expects a parameter representing the ID of the control you want to find.

You do not have a control with the ID Game_1 inside your repeater, so FindControl will return null, and you will get a NullReferenceException.

You need to pass in the correct ID for the control you''re trying to find:

bool itemSelected = false;
foreach (RepeaterItem ri in myRepeater.Items)
{
    RadioButton rb = (RadioButton)ri.FindControl("rdOption1");
    if (rb.Checked) 
    {
        itemSelected = true;
        break;
    }
}

args.IsValid = itemSelected;


NB: This will validate whether "Option_1" is selected in any item. If that''s not what you''re trying to validate, then you''ll need to explain your requirement.

EDIT: If you want to validate that one of the radiobuttons is selected in each item:

args.IsValid = true;

foreach (RepeaterItem ri in myRepeater.Items)
{
    RadioButton rb = (RadioButton)ri.FindControl("rdOption1");
    if (rb.Checked) continue;
    
    rb = (RadioButton)ri.FindControl("rdOption2");
    if (rb.Checked) continue;
    
    // Neither option is selected:
    args.IsValid = false;
    break;
}


这篇关于如何在C#中验证ASP.NET中继器中的多个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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