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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Onclick attribute firing before Validation Control

Status
Not open for further replies.

sburden

Programmer
Dec 4, 2002
35
US
Hi Folks,

I have a situation where the OnClick attribute is firing before my validation controls and it's causing an error when attempting to add a row to my table. I generate this error when I key in an invalid zip code(123A5). How can I prevent this from happening?

<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" Width="60px" />

<asp:TextBox ID="txtZIPCOD" runat="server" Width="171px" MaxLength="5"></asp:TextBox></td>

<td align="left" style="width: 325px" valign="top">

<asp:RangeValidator ID="Zip_Code_Validator" runat="server" ControlToValidate="txtZIPCOD" MaximumValue="99999"

MinimumValue="1" Type="Integer" Width="265px" SetFocusOnError="True" Display="None" ErrorMessage="Zip Code must be 5 digits (00001 - 99999)"></asp:RangeValidator></td>

 
Maybe try setting the btn's causesValidation property to True.
 
Hi jshurst,

Thanks for responding however, that's already activated.

Regards
 
Ah, sorry, misread the post. You coukd try creating a regular expression validator instead.
 
The problem is with the OnClick you have. Check this out.

Also, you may need to add a second REQUIRED validator, because it IS valid even if you leave it empty the textbox.
 
The action is handled by "Handles btnAdd.Click()". You should not add it again by the OnClick=""

You can use the OnClick= "..." in case you want to run a javascript
 
Hi TipGiver,

I'm new to asp.net and I'm not using code behind, I'm using code inline with the script tags so I'm actually running javascript.

Regards
 
If you're using 2.0 you can use "OnClientClick" for JS.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top