如何验证是否在C#中检查/选择RadioButtonList? [英] How to validate if a RadioButtonList is check/select it in C#?

查看:38
本文介绍了如何验证是否在C#中检查/选择RadioButtonList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从数据库填充的RadioButtonList,用户至少需要检查/选择一个RadioButtons.如何确保他们检查/选择RadioButton?我尝试了许多不同的方法,但是没有运气,这是我的代码.我还试图提醒他们取消选中单选按钮.

I have a RadioButtonList which is being populated from a Database, users need to at at least check/select one of the RadioButtons. How can I make sure they check/select the RadioButton? I tried many different ways but no luck, here is my code. I'm also trying to alert them of the uncheck radiobutton.

我要执行的操作是验证是否选中了RadioButtonList.

What I'm trying to do is to validate the RadioButtonList is checked/selected.

    if (cblstatus.SelectedItem.Value == "1")
    {
        //Create Category
        con.Open();
        SqlCommand departament = new SqlCommand("insert into categories(ca_name, ca_number, ca_status, ca_department, ca_date, ca_time, ca_user) " +
        "values('" + category.Text + "', '" + number.Text + "', 'Y', '" + catdepartment.SelectedValue + "', '" + date + "', '" + time + "', '" + user + "')", con);
        departament.ExecuteNonQuery();
        con.Close();
    }
    else if (cblstatus.SelectedItem.Value == "2")
    {
        //Create Category
        con.Open();
        SqlCommand departament = new SqlCommand("insert into categories(ca_name, ca_number, ca_status, ca_department, ca_date, ca_time, ca_user) " +
        "values('" + category.Text + "', '" + number.Text + "', 'N', '" + catdepartment.SelectedValue + "', '" + date + "', '" + time + "', '" + user + "')", con);
        departament.ExecuteNonQuery();
        con.Close();
    }
    else if (cblstatus. == null)
    {
        alert.InnerHtml = "<div id=\"warning\" class=\"message warning\">Warning! Please select a department.</div>" +
        "<script>" +
            "setTimeout(function () { $('#success').fadeOut(); }, 2000);" +
        "</script>";
    }

推荐答案

而不是在后面的代码中使用asp.net必需的字段验证器:

instead of doing it on the code behind use an asp.net required field validator:

这是一个示例:

<asp:RadioButtonList runat="server" ID="gender" RepeatDirection="Horizontal"  RepeatLayout="Flow" CssClass="labels">
<asp:ListItem Text="Male" Value="Male"></asp:ListItem>
<asp:ListItem Text="Female" Value="Female"></asp:ListItem>

<asp:RequiredFieldValidator runat="server" ID="genderRequired" Display="Dynamic"
    ControlToValidate="gender" ErrorMessage="This is an Error"
    ValidationGroup="signUp">*</asp:RequiredFieldValidator>

在后面的代码中,您可以创建一个空白的div,然后在出错时向其添加文本,然后可以使用jquery change函数对其进行进一步的操作:

on the code behind you can create a blank div and then add text to it on error and you can use the jquery change function to manipulate it further:

if (gender.SelectedItem.Value == "Male") 
{

    //do stuff
}

else if (gender.SelectedItem.Value == "Female")
{
    //do stuff
}

else 
{
    errorDiv.InnerText = "Error Messeage";
}

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.requiredfieldvalidator.aspx

这篇关于如何验证是否在C#中检查/选择RadioButtonList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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