Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp:RequiredFieldValidator makes postback with empty fields

I've faced the following problem with my aps validator. I have a textbox with asp validator. When the user leaves the textbox empty and click on the submit button the validation message shows, but the page does a postback. Any ideas what may cause this happen?

Heres the validator:

<asp:RequiredFieldValidator ID="valReqName" runat="server" 
    ControlToValidate="txtName" Display="Dynamic" ErrorMessage="blq blq">
</asp:RequiredFieldValidator>

<asp:TextBox ID="txtName" style="font-family:Tahoma, Geneva, sans-serif; 
    color: #4F4F4F;" runat="server">
</asp:TextBox>

Many thanks, Anton

EDIT

I have two tabs in an UpdatePanel. The validation problem is part of the second tab, where the user makes some inputs. When I've remove this trigger:

<asp:AsyncPostBackTrigger ControlID="lnkUpload" EventName="Click" />

The postback problem was solved, but another problem occurred. Well at least the postback is removed. Thanks for all answers.

(lnkUpload is the id of the second tab linkbutton)

EDIT 2

Well here is the solution. It appears that my problem was the same like in this article. http://jeffreypaarhuis.com/2011/08/08/validation-not-working-in-updatepanel/

like image 806
Anton Belev Avatar asked Jul 30 '12 05:07

Anton Belev


3 Answers

I'm posting the solution as it might be useful for someone else.

"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>

It appears that my problem was the same like in this article.

like image 77
Anton Belev Avatar answered Nov 18 '22 08:11

Anton Belev


Have you tried the solution in this thread:

ASP.net RequiredFieldValidator not preventing postback

Remove the following line from web.config

 <xhtmlConformance mode="Legacy"/>

"If I remove the line, my validation works the way I expected it to. Googling that uncovered a bunch of blog posts about how VisualStudio adds that line to the web.config when upgrading web apps from .net 1.1 to .net 3.5.

The blog posts were mainly complaining about how that field interferes with .net's AJAX stuff, but I'm guessing it messes with the JavaScript emitted for the RequiredFieldValidator in a similar fashion."

like image 25
JoonasL Avatar answered Nov 18 '22 09:11

JoonasL


Please add ValidationGroup to your validator(valReqName) and button.

like image 41
Narendra Avatar answered Nov 18 '22 08:11

Narendra