Validation not working in updatepanel

August 8th, 2011 | Posted by Jeffrey Paarhuis in Developing

Ok, another day, another problem!

I did create a few usercontrols which work together quite nicely. And after I wrapped an updatepanel around them, the ajax seems to work just fine.

However, once a validator inside the panel gets triggered the whole page freezes and it looks like javascript isn’t working anymore. I googled (cause bing just sucks) the problem and found very less about this problem, so when I finally found a solution I decided to share it with all of you having the same problem.

This is the problem:

When a validator is loaded on the page it creates a bit of javascript to support the clientside validation. When you place a validator inside an usercontrol that isn´t visible by default, and this usercontrol is in an updatepanel, it does not create that javascript properly.

This is the solution:

Outside the updatepanel, I did above, create a dummy validator with a dummy textbox using a dummy validationgroup like so:

<%--dummy validator to make ajax validation possible--%>
<asp:RequiredFieldValidator runat="server" CssClass="hidden" ControlToValidate="dummyTextBox" ValidationGroup="dummy"></asp:RequiredFieldValidator>
<asp:TextBox runat="server" ID="dummyTextBox" CssClass="hidden"></asp:TextBox>

The CssClass “hidden” corresponds with the following CSS:

<style>
.hidden {
  display: none;
}
</style>

As you already might understand, the dummy validator now creates the proper javascript that the other ‘real’ validators can use.

Hope this also works for you. Also if you found another, maybe neater solution, please inform me…

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

8 Responses



Leave a Reply to Sutha.K Cancel reply

%d bloggers like this: