Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
User Controls When to set the value of controls
Message
De
28/07/2009 08:53:06
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
ASP.NET
OS:
Windows XP SP2
Application:
Web
Divers
Thread ID:
01414924
Message ID:
01415027
Vues:
38
Hi Paul,

>>Hi All,
>>
>>I have created a couple of user controls that I want to dynamically add to a page. The first issue is I was under the impression I had to set the value I wanted in my controls within the Page_PreRender as opposed to the Page_Load event. When the Page PreRender fires, my controls are still null. What is the common wisdom of when to set my controls values?
>
>Page_Load. PreRender happens too late.

The book I have indicates the PreRender is definately the place but as you say it doesn't work there. I moved my code to the page load and it works just fine there. But if the PreRender is too late, why are the controls still null?

>
>>The second question is can I place a User Control in a PlaceHolder? It doesn't throw any exceptions, but doesn't display. I need to place one of two controls in a specific location on my page and don't know which control until the data is retrieved but before the page is loaded. What is the best way to do this?
>
>I'd think strongly about whether dynamically added controls were really necessary, or if you could just get away with putting multiple controls on the page and selectively making them visible/invisible. It can be a PITA to get the eventing correct when you add controls dynamically and it's the kind of thing you might not notice immediately. If you really think you need to go this route, I can follow up with more info (for example, you'll definitely want to use a placeholder for a dynamically added control).

This is a surprise to hear. I did get this working just fine, but I was sure under the impression that common wisdom indicates using seperate controls was a good thing for keeping a page de-cluttered and for maintenance. For now, I have them working but guess I will do enough testing to make sure there isn't a problem. They are in an ajax panel so they only refresh this portion. The problem is there are two different controls for different situations. It is a bit of a mess to keep all that junk in the page and deal with hiding and unhiding these controls.

I did get them working in the place holder but I had to change my code from this:

ctrlMyControl myControl = new ctrlMyControl();
this.placeholder1.Controls.Add(myControl);

to this:

ctrlMyControl myControl = (ctrlMyControl)Page.LoadControl("~/UserControls/ctrlMyControl.ascx");
this.placeholder1.Controls.Add(myControl);


Thanks for your comments.
Tim
Timothy Bryan
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform