Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

25 Experts available now in Live!

Get 1:1 Help Now