?
Solved

radiobuttonlist validation

Posted on 2008-10-08
8
Medium Priority
?
1,321 Views
Last Modified: 2012-05-05
I'm writing a custom validation for a radiobutton list (server-side), but it doesn't seem to be working.

Here is the radiobuttonlist code:

<asp:CustomValidator
            ID="valxGuest"
            runat="server"
            ControlToValidate="rblGuest"
            EnableClientScript="false"
            SetFocusOnError="true"
            ErrorMessage="<span class='alert'>Guest Attending?</span>"
            Display="dynamic"
            Enabled="False" OnServerValidate="valxGuest_ServerValidate" ForeColor=""></asp:CustomValidator>  
        <span><label for="currcust" id="lblGuest" runat="server">Guest Attending?</label></span>
                <asp:RadioButtonList
                    ID="rblGuest"
                    runat="server"
                    CssClass="radiolist"
                    RepeatColumns="2"
                    RepeatLayout="flow"
                    RepeatDirection="horizontal">
                   <asp:ListItem Selected="false" Text="Yes" Value="yes" />
                   <asp:ListItem Selected="false" Text="No" Value="no" />
                </asp:RadioButtonList>

and here is the custom method I wrote to validate the radiobuttonlist:

protected void valxGuest_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (rblGuest.SelectedValue.ToString().ToLower() == "yes" || rblGuest.SelectedValue.ToString().ToLower() == "no")
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }


Anyone see where I went wrong?

Thanks for any help.
0
Comment
Question by:-Dman100-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 2000 total points
ID: 22671985
Hi,

Enabled = "False" maybe? Should be Enabled = "True" in order for the validator to fire?

/Carl.
0
 

Author Comment

by:-Dman100-
ID: 22672012
Hi Carl,

Thanks for replying to my post.  I just spotted the error:

I had:

I'm writing a custom validation for a radiobutton list (server-side), but it doesn't seem to be working.

Here is the radiobuttonlist code:

<asp:CustomValidator
            ID="valxGuest"
            runat="server"
            ControlToValidate="rblGuest"
            EnableClientScript="false"
            SetFocusOnError="true"
            ErrorMessage="<span class='alert'>Guest Attending?</span>"
            Display="dynamic"
            Enabled="False" OnServerValidate="valxGuest_ServerValidate" ForeColor=""></asp:CustomValidator>  


I needed to remove the control to validate since it is a custom validator.

So, it should have been this:

I'm writing a custom validation for a radiobutton list (server-side), but it doesn't seem to be working.

Here is the radiobuttonlist code:

<asp:CustomValidator
            ID="valxGuest"
            runat="server"
            EnableClientScript="false"
            SetFocusOnError="true"
            ErrorMessage="<span class='alert'>Guest Attending?</span>"
            Display="dynamic"
            Enabled="False" OnServerValidate="valxGuest_ServerValidate" ForeColor=""></asp:CustomValidator>  


It's working now.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22672067
Are you trying to force user to make at least one selection from the RadioButtonList?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:-Dman100-
ID: 22672777
Hi prairiedog,

yes, that is correct.  At least one selection.  It appears to be working okay now once I removed the ControlToValidate attribute.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22672842
You are reinventing the wheel. The built-in RequiredFieldValidator will do that for you.
0
 

Author Comment

by:-Dman100-
ID: 22673206
Hmmm, I tried it with the required field validator and it did not work?  I've used the required field validator many times, so I'm not sure what I would have done incorrect?
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22673254
Here is what I used in my code:

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
            <asp:ListItem>Yes</asp:ListItem>
            <asp:ListItem>No</asp:ListItem>
        </asp:RadioButtonList>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="RadioButtonList1"
            ErrorMessage="Guest Attending?"></asp:RequiredFieldValidator>

Open in new window

0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22673333
Modified my RadionButtonList to exactly match yours. Still working.
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
	<asp:ListItem Selected="false" Text="Yes" Value="yes"></asp:ListItem>
	<asp:ListItem Selected="false" Text="No" Value="no"></asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="RadioButtonList1"
	ErrorMessage="Guest Attending?"></asp:RequiredFieldValidator>

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question