Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, got a problem with the standard asp.net validators in a project.

Basically I am adding the following to my form:

XML
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="MyGroup" CausesValidation="true"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" EnableClientScript="true" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" ValidationGroup="MyGroup"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="MyGroup" CausesValidation="true" onclick="Button1_Click" />


Problem is although the validation error displays, the page still posts back.

It is at this point i will say that if i put this in an .aspx page in the same project it works as it should.

However when i put it in a user control which is dynamically rendered, i get this problem.

The project does use URL rewriting as well, but im not sure that this is causing it. Any ideas?
Posted

1 solution

As it turns out it was to do with the URL re-writing. I had Quickly implemented a custom form control off an MSDN article to combat the re-writing on the post back. What the control did was to remove the url from the action attribute, however it also removed the
on submit action.
So to solve the problem the following needs to be added to the RenderAttributes override in the custom control, should anyone else be using it.


C#
if(this.Page.Validators != null && this.Page.Validators.Count >=1)
    writer.WriteAttribute("onsubmit", "javascript:return WebForm_OnSubmit();");
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900