Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validation

Status
Not open for further replies.

nc297

Programmer
Apr 7, 2010
162
US
How can I not allow the form to go through if the user selects "other" from the DDL and not enter in something in the textbox to describe other:


How did you hear about us:

<asp:RadioButtonList ID="RadionButtonList1" runat="server">
<asp:ListItem>Newpaper Ad</asp:ListItem>
<asp:ListItem>Radio</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:RadioButtonList>
<br />
<br />
<asp:TextBox ID="textbox1" runat="server"></asp:TextBox>

<asp:Button ID="Button2" runat="server" Text="Click" />
 
use CustomValidator and provide a handler for ServerValidate.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I've tried the customvalidator but nothing is happening. Is this set up the correct way?

<asp:DropDownList ID="PhilDDL" runat="server">
<asp:ListItem Value="Website">Website</asp:ListItem>
<asp:ListItem Value="Contents">Contents</asp:ListItem>
<asp:ListItem Value="Design">Design</asp:ListItem>
<asp:ListItem Value="Ease of Use">Ease of Use</asp:ListItem>
<asp:ListItem Value="Links">Links</asp:ListItem>
<asp:ListItem Value="Navigation">Navigation</asp:ListItem>
<asp:ListItem Value="Timeliness">Timeliness</asp:ListItem>
<asp:ListItem>(other)</asp:ListItem>
<%--<asp:ListItem Selected="True" Value="0">Select a Comment</asp:ListItem>--%>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ControlToValidate="PhilDDL" ErrorMessage="make a selection" InitialValue="0"
SetFocusOnError="True"></asp:RequiredFieldValidator>



<asp:TextBox ID="PhilaCommtxt" runat="server"
Height="91px" Width="203px" ToolTip="Explain Other Comments Box" MaxLength="50"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="Explain Other" OnServerValidate="ServerValidate" ></asp:CustomValidator>

Protected Sub ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator2.ServerValidate
If (Me.PhilaCommtxt.Text.Equals(String.Empty) And Me.PhilDDL.SelectedValue.Equals("(other)")) Then
args.IsValid = False
Else
args.IsValid = True
End If

End Sub
 
that looks correct. do you verify the page is valid when you post back (typically a button click)?
Code:
void DoWork(object sender, EventArgs e)
{
   if(IsValid == false) return;

   do something with valid state
}
this is in addition to the custom validator code.

set a break point on ServerValidate and the "DoWork" methods and make sure you are stepping through the code.

Also note that the server validation will only execute on the server, so if you have failed validation on the client the server validation will not execute. once the client is valid, then the request is sent to the server and the server validation executes.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Okay that makes sense I published it and ran it on the server side and it caught it. That was my problem. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top