Solved

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

Posted on 2011-03-25
3
491 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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now