473,378 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 ValidatorCollection. Apparently they're not or I'm trying to read them at the wrong time in the page life cycle.

Thanks for any help.
Aug 24 '11 #1
5 2838
Frinavale
9,735 Expert Mod 8TB
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
Aug 25 '11 #2
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.
Aug 25 '11 #3
Frinavale
9,735 Expert Mod 8TB
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
Aug 25 '11 #4
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.Validators.Add(validatorX) 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
Aug 26 '11 #5
Frinavale
9,735 Expert Mod 8TB
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.
Sep 1 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Alphonse Giambrone | last post by:
I have a web form with 2 user controls on it (UC1 and UC2). Each control has a bound datagrid with textboxes in the footer to add a new row. There are also requiredfieldvalidators in each footer....
4
by: Dot net work | last post by:
If I have got 2 web user controls on my aspx form, and one web user control has got some validator controls on it, what I find is that if I enter in some "bad data" in to some text boxes on the...
1
by: Robert Howells | last post by:
Perhaps I'm just too new at this to pull it off, or perhaps it's just bad architecture. I'd appreciate some feedback on the the wisdom (or lack thereof) in attempting the following: I'm not new...
0
by: jonelling | last post by:
I am having a problem where the page load event is not being fired for certain user controls that I load dynamically in placeholders. Here is what I'm doing in brief, with full test code supplied...
1
by: Demetri | last post by:
I'm trying to determine if we want to use panels or user controls for our pages. Our primary concern is performance, page loading and posting speed. To illustrate my question, lets use the...
0
by: MattB | last post by:
I have a (asp.net 1.1) page that loads a bunch of user controls based on some database data. The User Controls have input controls and associated validators on them, and I wanted to place a...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
1
by: Kyle K. | last post by:
Environment: Visual Studio 2005 + WinXP Pro SP2 + .NET 2.0 I am new to creating user controls and have a (hopefully) simple question. I have a series of personal questions (first/last name,...
2
by: tbh | last post by:
hi, hope this cross-post is OK. it's unclear to me whether this question belongs more under vstudio or dotnet... i'm using VS2005 pro and am one co-developer of a web solution that is getting to...
2
by: Hillbilly | last post by:
Anybody have any sage advice on this frustrating "feature" of ASP.NET? I have a TextBox in the EditItemTemplate of a DataList I can't seem to find for some reason that is I believe related to the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.