社区首页 >问答首页 >如何在C#中验证某个RadioButtonList是否处于选中状态?问如何在C#中验证某个RadioButtonList是否处于选中状态?ENStack Overflow用户提问于 2012-01-07 08:40:48回答 5查看 86K关注 0票数 6我有一个从数据库填充的RadioButtonList,用户至少需要检查/选择其中一个RadioButtons。如何确保他们选中/选择RadioButton?我尝试了许多不同的方法,但没有成功,以下是我的代码。我也在试着提醒他们取消选中单选按钮。
我尝试做的是验证RadioButtonList是否已选中/选中。
代码语言:javascript运行复制 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 = "
" +"";
}asp.net.netc#关注问题分享EN回答 5推荐最新Stack Overflow用户发布于 2012-01-07 08:53:45
使用asp.net required字段验证器,而不是在代码后面执行此操作:
下面是一个示例:
代码语言:javascript运行复制
代码语言:javascript运行复制 ControlToValidate="gender" ErrorMessage="This is an Error" ValidationGroup="signUp">*
代码语言:javascript运行复制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
收藏分享票数 14ENStack Overflow用户发布于 2014-03-25 20:45:58
我们可以在不定义InitialValue属性的情况下使用普通的RequiredFieldValidation
代码语言:javascript运行复制
收藏分享票数 5ENStack Overflow用户发布于 2015-10-02 17:23:28
可以使用这种方法,但完全是代码隐藏。
代码语言:javascript运行复制 public bool ValidateRadioButtonList()
{
bool rbchecked= false;
for (int i = 0; i < RadioButtonList1.Items.Count - 1; i++)
{
if (RadioButtonList1.Items[i].Selected==true)
{
rbchecked = true;
}
}
if (rbchecked == true)
{
lblError.Text = string.Empty;
return true;
}
else
{
lblError.Text = "Please select RadioButton!";
return false;
}
}收藏分享票数 4EN查看全部 5 条回答页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持原文链接:https://stackoverflow.com/questions/8766324
复制相关文章