How can I use a custom validator on a radio button list in ASP.NET and C#?

I need to do custom validation on my controls so I can specify colors for things that do or do not pass validation.  I'm having trouble on a RadioButtonList.  I need to write something that basically does required field validation -- if neither button is checked, validation fails.  See the code for what I have so far.

My radiobuttonlist controls whether or not some other things are enabled or disabled -- hence the onselectedindexchanged.

Ideas?
<asp:RadioButtonList ID="rdoInternetAtHome" runat="server" RepeatDirection="Horizontal" CssClass="options" onselectedindexchanged="rdoInternetAtHome_SelectedIndexChanged" AutoPostBack="true">
  <asp:ListItem Text="Yes" Value="1" />
  <asp:ListItem Text="No" Value="0" />
</asp:RadioButtonList>



<asp:CustomValidator ID="vldInternetAtHome" runat="server" 
            ErrorMessage="Please specify whether or not you have Internet service at home." 
            Display="None" onservervalidate="vldInternetAtHome_ServerValidate" />
		

		
protected void vldInternetAtHome_ServerValidate(object source, ServerValidateEventArgs args)
{
  if ((rdoInternetAtHome.SelectedItem.Value != "1") || (rdoInternetAtHome.SelectedItem.Value != "0"))
    {
      args.IsValid = false;
      rdoInternetAtHome.ForeColor = Color.Red;
    }
    else
    {
      args.IsValid = true;
      rdoInternetAtHome.ForeColor = Color.Green;
   }
}

Open in new window

mrcoulsonAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
masterpassConnect With a Mentor Commented:
Try this
protected void vldInternetAtHome_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = false;
if(rdoInternetAtHome.SelectedItem!=null)
{
  if ((rdoInternetAtHome.SelectedItem.Value != "1") || (rdoInternetAtHome.SelectedItem.Value != "0"))
    {
      args.IsValid = false;
      rdoInternetAtHome.ForeColor = Color.Red;
    }
    else
    {
      args.IsValid = true;
      rdoInternetAtHome.ForeColor = Color.Green;
   }
}
}

Open in new window

0
 
masterpassCommented:
A small re-write would do the trick
protected void vldInternetAtHome_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = false;
  if ((rdoInternetAtHome.SelectedItem.Value != "1") || (rdoInternetAtHome.SelectedItem.Value != "0"))
    {
      args.IsValid = false;
      rdoInternetAtHome.ForeColor = Color.Red;
    }
    else
    {
      args.IsValid = true;
      rdoInternetAtHome.ForeColor = Color.Green;
   }
}

Open in new window

0
 
mrcoulsonAuthor Commented:
"Object reference not set to an instance of an object."

Relevant stuff from stack trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   InternetSurvey._Default.vldInternetAtHome_ServerValidate(Object source, ServerValidateEventArgs args) +75

Jeremy
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mrcoulsonAuthor Commented:
It must have something to do with:

if ((rdoInternetAtHome.SelectedItem.Value != "1") || (rdoInternetAtHome.SelectedItem.Value != "0"))

When I change it to the following, I do not get the error.  Of course, it doesn't work properly, but I skip the error.

if (rdoInternetAtHome.SelectedItem.Value != "1")

Maybe it's because no selection has been made and therefore SelectedItem doesn't have a value at all to test.

Jeremy
0
 
mrcoulsonAuthor Commented:
Hey, that got it!

Jeremy
0
 
masterpassConnect With a Mentor Commented:
I'm Glad that it worked !!!!

like what you said "it's because no selection has been made and therefore SelectedItem " was null .. A check solved the error
0
 
mrcoulsonAuthor Commented:
I made a small change to what is attached.  It just made more sense.  If the selected item is null, do this stuff.  I didn't realize I could test for nullitude of the selected item, so thanks for putting me on the right track!

Jeremy
protected void vldInternetAtHome_ServerValidate(object source, ServerValidateEventArgs args)
{
  args.IsValid = false;
  if (rdoInternetAtHome.SelectedItem == null)
    {
      args.IsValid = false;
      rdoInternetAtHome.ForeColor = Color.Red;
    }
    else
    {
      args.IsValid = true;
      rdoInternetAtHome.ForeColor = Color.Green;
    }
}

Open in new window

0
 
mrcoulsonAuthor Commented:
Thanks a ton!  I learned, I got the right answer. and I worked through a little logic.  Not bad at all!

Jeremy
0
All Courses

From novice to tech pro — start learning today.