Finding Validators in User Controls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AllBeagle
    New Member
    • Sep 2008
    • 13

    Finding Validators in User Controls

    Hello All,

    I have a scenario where I have a registration page with a dozen or so user controls that get loaded dynamically. During the load each of these user controls each has a number of custom containers that loads validators based on the container's attributes. The problem I'm having is when I do a search like such...

    For Each Val as IValidator in Validators
    ....
    Next

    ...I'm only seeing Validators at the page level, none of the validators show up that are part of a user control. I'm not able to post source code at this time (sorry I know that really makes it harder), but if anyone has an idea of why you wouldn't be able to see user control validators, I'd really appreciate any feedback. The validators work as they should, I just can't find them when iterating through the controls and/or validators during init, load, or pre-render. I also thought maybe they were being assigned a validation group, but that's not the case either, they're not which led me to assume they should all be going into the Page ValidatorCollec tion. Apparently they're not or I'm trying to read them at the wrong time in the page life cycle.

    Thanks for any help.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Have you considered creating a public property for each user control that exposes it's validators to the parent page?

    That way you would loop through the page's validators, and loop through your user controls to retrieve the validators to do things with it.

    I'm not sure why you're looping through the validators though.
    Are you trying to determine if the page is valid server-side?
    If so, doesn't the checking the Page.IsValid property work?


    -Frinny

    Comment

    • AllBeagle
      New Member
      • Sep 2008
      • 13

      #3
      What exactly do you mean by "exposes it's validators to the parent page"? I have kind of a unique situation with the validators where if a user meets certain criteria they can bypass most of them. So I'm trying to find a way to first loop through all of them and turn off the client script (so I can get back to the server) and then on the server side loop through them to see if certain ones are valid. Then finally take the ones that are not valid and put them in a summary for an alert. Kind of like a warning to the user, but they have privileges to save anyway if they so choose. The problem remains though where I can't see any of the validators inside the user controls.

      The validators themselves are created dynamically though so I'm starting to get worried that they are getting created too late in the cycle for me to see them. The user controls consist of custom controls (basically that act as containers) and during PreRender (of the custom control) these containers have validators built dynamically depending on their properties.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        So, from what I understand, you are dynamically creating Validators in the UserControls.

        You want to disable some of these Validators if the user meets some sort of criteria.

        Since you are dynamically creating the validators, you can simply add them to a List of Validators while you are creating them (in each UserControl). Then in each UserControl you can make a Public ReadOnly Property that returns this list of validators.

        Now, in your Page code you can access this property of the user control and loop through the validators.

        Make sense?


        What I would probably do in your situation is access the logged-in-user from the UserControl itself and check the criteria as I'm creating the validators.

        :)

        -Frinny

        Comment

        • AllBeagle
          New Member
          • Sep 2008
          • 13

          #5
          I appreciate your input and you've definitely got me thinking of how to approach this. Needless to say I'm still pulling my hair out, but I think I'm on the right track.

          The custom containers I referred to before are in another assembly and inherit from Panel. These containers are coded directly on the .aspx/.ascx pages and are populated with your normal input controls (textbox, etc). On PreRender the custom container checks out it's children and their properties (in addition to some of it's own) and builds the validators that way. So what I did was add a property, List(Of IValidator), to the container that is supposed to get filled with the validators as they are built.

          So I set my breakpoints and watch as it goes through creating all the validators and I can see it hit the .Add perfectly fine. Then I come back to my page, loop through my containers (also on PreRender), check their new properties and all I get is empty collections. I'm losing the contents somewhere along the way or I'm back to my problem where I'm thinking the PreRender creation of the validators is not allowing me to grab them on the PreRender of the Page. Does that make sense? Does Page PreRender occur before the User Control PreRender? Because if that's the case, my property will always be empty because the user control hasn't populated it yet.

          I also tried to do a Me.Page.Validat ors.Add(validat orX) and it still doesn't add them. I wasn't sure how I could add it efficiently to a user control property since I'm loading the validators outside the user control. I don't want to have to recursively look for more controls than I already am.

          Again, I really appreciate all of your help on this. I'm trying to get an understanding of what it is I'm actually doing so a drawn-out response other than "use code like this" is nice for once. FYI too, I did not create the original code, I'm trying to build and expand on without having to completely knock walls down, otherwise I would just do things completely differently with the validation. :o

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            I can't remember if the controls' PreRender is called before the Page's PreRender...

            Do you have access to the code for the assembly that has the user controls in it?

            I really think it would be easier check the user's permissions within the user control itself rather than looping through things the way you are.

            Get rid of the property that returns the collection of validators. If you're using Forms Authentication, retrieve the logged-in user from the HttpContext Class in the PreRender event of the UserControl so that you can check the user's permissions and only add the validators that are required.

            If you aren't using Forms Authenticaiton, then you should probably create a property that will allow you to pass the current user into the user control (set this on the PageLoad for your page) so that you can check it in the UserControl's PreRender event. You don't even need to pass in the user...just have properties for your UserControl that can be set by the page (early in the page's life cycle) so that the UserControl can configure itself.

            I truly think this is going to be the better approach in the long run.

            -Frinny

            PS I'm sorry for the delayed response but I've been away for a few days.
            Last edited by Frinavale; Sep 1 '11, 02:45 PM.

            Comment

            Working...