Solved

Asp:RequiredFieldValidator cause validation on Server even if validator is disabled

Posted on 2011-03-25
3
500 Views
Last Modified: 2012-05-11
Hi there, I will keep this as short as possble but its wrecking my head. I have a user control with a label and a field. I add this dynamically to a page at run time. There could be many. The user control has a RequiredFieldValidator and looks all like this. The validator is enabled by default.

    <div class="controlCell">
        <asp:TextBox ID="dfControl" runat="server" Text="" ToolTip="ucsAlphanumericEntryDC" Width="95%" CausesValidation="true"/>
        <asp:RequiredFieldValidator ID="rfvContol" runat="server" ErrorMessage="Value is required" EnableClientScript="true" 
            ValidationGroup="ValidateContols" ControlToValidate="dfControl" Display="Dynamic" Enabled="true"></asp:RequiredFieldValidator>
    </div>

Open in new window


When I dynamically load the control I check a parameter to tell me if the field is required or not. The paramater is from the database for this control that I am about to draw. If the Field NOT a required field I disable the validation in the page_preRender.. also tried it in load.

               if (!DisplayRequired)
                {
                    rfvContol.EnableClientScript = false;
                    rfvContol.Enabled = false;
                    dfControl.CausesValidation = false;
                }

Open in new window


So far so good. This all works on the client side validation for only the user controls with the Display Required set to true, leaving the Validator enabled.

however on post page from a submit button which I have wired to the same validation group as the user controls, I check the IsValid on the submit and if its false I dont save anything. But when the page re-renders on the client.. I have error messages on all the user controls, even though I disabled them.

Any idea.. thanks
0
Comment
Question by:MickyMc
3 Comments
 
LVL 7

Accepted Solution

by:
nbove earned 500 total points
ID: 35218862
This seems like a ViewState issue.  Do you have viewstate disabled for the page?  What it sounds like is that the dynamic user controls are not preserving their "Required" state between postbacks.  Typically this would be handled by viewstate, but in the case of dynamic controls, things can get tricky.

The following article has a pretty good explanation about viewstate:
http://msdn.microsoft.com/en-us/library/ms972976.aspx
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35877221
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question