Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
try
{
if (btnButton1.Checked == "")
{ throw new Exception("Please provide a rating"); }
}

catch (Exception ex)

{
Response.Write(ex.Message);
}

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.
Posted
Updated 16-May-12 21:39pm
v2

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...
 
Share this answer
 
Comments
Member 14189690 20-Mar-19 11:32am    
I have similar question. I have around 70 radio buttons. I want to check if all the radio buttons are checked then to submit something. If one single radio button is not checked then don't submit anything until the user checks that button
OriginalGriff 20-Mar-19 11:54am    
Why do you think that having 70 radio buttons on the same form is a good idea?
Do you think the user wants to check that many in one go?

You can do it - just loop through all the radio buttons on the parent container - but from a user interface POV it's a pretty poor solution. I probably wouldn't use your app twice, if you know what I mean ...
Member 14189690 20-Mar-19 12:04pm    
I am doing evaluation project. So I have a questionnaire containing 15 question on which user has to select one option from 1 to 5. I use radio buttons for those options. But how to prevent submitting those answers if there is one unchecked radio button
OriginalGriff 20-Mar-19 12:14pm    
I have to say that sounds like a "student solution" - and that's not a good thing! :laugh:

When was the last time you saw an electronic survey with 15 questions on the same form or page?
It's a bad idea because it's a "lump" to look at - and that intimidates people, and will mostly cause them to close the app and go do something less boring. Two, three questions per page (or even one if the question text is more than a short sentence) is much better from a psychological POV - provided you give an idea how far through they are - and allows you to skip irrelevant options.
Member 14189690 20-Mar-19 12:22pm    
I get you. But its not my call on the questions. I was given the template and I have to implement it. So, back to the question on validating before submitting, any solution?
C#
if (!btnButton1.Checked)
{
 throw new Exception("Please provide a rating"); }
}
 
Share this answer
 
v2
Comments
Oshtri Deka 17-May-12 4:59am    
Edit: ! operator
Try using:

C#
if (btnButton1.Checked == Checkstate.Checked)
 
Share this answer
 
v2
C#
try
{
  if (!btnButton1.Checked == true)
     {
       throw new Exception("Please provide a rating");
     }
}
 
catch (Exception ex) 
{
   Response.Write(ex.Message);
}


Try this 1. Hope it helps. :)

-dabeeyow
 
Share this answer
 
RadioButtion always return true when checked and return false if uncheck.

make a good practice for naming controls.

According to you if there are radio buttons and check boxes in your form better to name like rbGender.Checked rather using btnButton1.Checked.

C#
if(rbGender.Checked==true)
{
//do what you want when checked
}
else
{
//do what you want when not checked
}



mark it answer if solves your problem and dont forget to rate..

Thanks
 
Share this answer
 
Super! Thank you for this. I replaced the empty string with false and that particular problem worked well now.

So I tried testing this by not selecting a radio button and the message "Please provide a rating" was returned.

However, I still got this message when I did select a radio button.

I'm sure I'm missing something basic here.
 
Share this answer
 
Comments
Jim Jos 17-May-12 5:03am    
Please use step by step debugging and find out where the exception is generated.. Exception is the mother of all exceptions and you are using it to show your radiobutton error. Best thing is creating your own exception called RadioButtonNotClickedException and try it out..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900