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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

masterpassCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mrcoulsonAuthor Commented:
Hey, that got it!

Jeremy
0
masterpassCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.