验证C#中的单选按钮和复选框 [英] Validate radio buttons and check boxes in C#
本文介绍了验证C#中的单选按钮和复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望有人能够帮助我.
我在提交"按钮上有一条try catch语句,希望验证一系列单选按钮和复选框.
内容如下:
I hope someone will be able to assist me.
I have a try catch statement on the submit button hoping to validate a series of radio buttons and check boxes.
It goes as follows:
try
{
if (btnButton1.Checked == "")
{ throw new Exception("Please provide a rating"); }
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
从该消息看来,该操作员无法使用,并且我也不确定所选择的.Checked选项.
请协助.
谢谢.
From the message it appears the operator cannot be used, and I am also not sure about the .Checked option I chose.
Please assist.
Thank you.
推荐答案
RadioButton.Checked属性是一个布尔值:如果选中,则为true
,否则为false
.
因此,将它与空字符串进行比较永远是行不通的...
The RadioButton.Checked property is a bool :true
if checked,false
if not.
So comparing it against an empty string is never going to work...
if (!btnButton1.Checked)
{
throw new Exception("Please provide a rating"); }
}
尝试使用:
Try using:
if (btnButton1.Checked == Checkstate.Checked)
这篇关于验证C#中的单选按钮和复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文