Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load a User Control Programmatically & retrieving set property values

Status
Not open for further replies.

onedizzydevil

Programmer
Mar 24, 2001
103
US
I find code that show how to load a User Control programmatically; even ones that show how to set the property(ies) AS YOU CREATE the control. However, what I need is know how to retreive the property value inside of a user control.

For example, if I have a user control and button (not in the user control) that needs to toggle the isVisible property of the User Control, NOT the visiblity of a panel, table or anything else AND I don't want to put it in one.

How can I get the current value of the isVisible property?

Any ideas?

Wayne Sellars

"Programming, today is a race between software developers, striving to build bigger and better idiot-proof programs, and the Universe, trying to produce bigger and better idiots. So far, Universe 1 - Programmers 0."
 
It depends on how you store that property. But I'll give you a for instance.

In my apps, I normally need to store user input, and I do that in viewstate, typically through textboxes, dropdowns, and so on. You could store the visible property in the statebag, as well, which is why I offer this.

In order to be able to retrieve properties of a programatically loaded user control, you must load it from the parent in the page_init event. The reason is because the viewstate is loaded after that.

So that the parent loads it dynamically on every load of the page, and then the user control is "smart" enough to go and grab its values from the viewstate when that event raises.

Then, those values are freely available in any event subsequent to the load_viewstate, such as page_load, or any other user raised event, like a button_click (in your case).

So:
Code:
protected withevents myUC as myUserControl

page_init
myUC = LoadControl("../userControls/myUserControl.ascx")
somePlaceHolder.Controls.Add(myUC)

page_load
if myUC.visible then....

This progression of events should work for you just fine.

Just store the visible property in the state bag for the user control.

Now, when you have a variable number of nested user controls on a page, things can get a bit trickier. What I will normally do is (still in the page_init) declare an array of userControls to the size that I know I will need, and jump in a loop, using LoadControl to instantiate them and then adding them to a placeholder as I go. You can then retrieve the properties in the way that I've shown up there, just that you'd be accessing the various elements of the array, which is where your individual objects reside.

Just remember that the events are the key to having the user controls remember who and what they are. This is the one thing that I found very sparse information for on the internet... although I can't understand why it's not given more treatment. Anyway...

hth! :)
paul
penny1.gif
penny1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top