-Dman100-
asked on
radiobuttonlist validation
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="rblGues t"
EnableClientScript="false"
SetFocusOnError="true"
ErrorMessage="<span class='alert'>Guest Attending?</span>"
Display="dynamic"
Enabled="False" OnServerValidate="valxGues t_ServerVa lidate" ForeColor=""></asp:CustomV alidator>
<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="horizonta l">
<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(o bject source, ServerValidateEventArgs args)
{
if (rblGuest.SelectedValue.To String().T oLower() == "yes" || rblGuest.SelectedValue.ToS tring().To Lower() == "no")
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
Anyone see where I went wrong?
Thanks for any help.
Here is the radiobuttonlist code:
<asp:CustomValidator
ID="valxGuest"
runat="server"
ControlToValidate="rblGues
EnableClientScript="false"
SetFocusOnError="true"
ErrorMessage="<span class='alert'>Guest Attending?</span>"
Display="dynamic"
Enabled="False" OnServerValidate="valxGues
<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="horizonta
<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(o
{
if (rblGuest.SelectedValue.To
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
Anyone see where I went wrong?
Thanks for any help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are you trying to force user to make at least one selection from the RadioButtonList?
ASKER
Hi prairiedog,
yes, that is correct. At least one selection. It appears to be working okay now once I removed the ControlToValidate attribute.
yes, that is correct. At least one selection. It appears to be working okay now once I removed the ControlToValidate attribute.
You are reinventing the wheel. The built-in RequiredFieldValidator will do that for you.
ASKER
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?
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>
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>
ASKER
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="rblGues
EnableClientScript="false"
SetFocusOnError="true"
ErrorMessage="<span class='alert'>Guest Attending?</span>"
Display="dynamic"
Enabled="False" OnServerValidate="valxGues
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="valxGues
It's working now.