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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User Controls and Parameters

Status
Not open for further replies.

jkb17

Programmer
Nov 27, 2000
156
US
I have created a user control that is a simple table. This table contains some drop down lists and will be dynamicly added to a web form based on a user's selection. I have the adding of the user contorl in place and it does react accordingly.

Here's my problem\question. Is it at all possible to pass a parameter to a user control. On my user control there are several drop down lists (2 i believe) that are populated from the database but would require an input parameter from the calling page.

Is there any way to send over an @ID to the user control. THere are multiple controls being added (could be as many as 5) that each may require an ID.

If this is feasible then great. If not then I can probably do something with the data grid instead.

I appreciate any assistance/tips/advice.

thanks

Jim
 
remember that a usercontrol is a class. So giving it parameters is as easy as it is in any other class:

Simply create a property:

private mvarmyPropery as string
public writeonly property myProperty as string
set(value as string)
mvarmyProperty = value
end set
end property

and then use the value in your user control as you see fit. You can set the value of that property declaratively:

<muc:myUserControl id=myUC runat=server myProperty=&quot;someString&quot; />

or programatically:

protected withevents myUC as myUserControl
~~~~~~~~~~
myUC.myProperty = &quot;someString&quot;

hth! :)
paul
penny1.gif
penny1.gif
 
Thank you, paul.

I did try that actually but I couldn't get it to work. Since the user control is added dynamically I may need to add the same control twice.

I had difficulty setting the property because it was not recognized.

I think I may be able to go the &quot;DataGrid&quot; route instead but its up to the client. They requested a specific format and that's why I had to create this table that would display details on an individual level. The details are encapsulated within a table which I thought would be logical to put into a UserControl as the # of tables\uc created are determined at run-time.

I was using the LoadControl() value in order to add them dynamically and I think that was the problem. I guess I need a way to dynmaically declare the usercontrol itself so I could get it to recognize the property from its intellisense.

If that makes sense.

thanks for you help.

bye
Jim
 
This definitely works. I use it every day.

Move the logic of your control into a new sub, rather than page_load. Make that sub public, and call it loadIt()
Use LoadControl to load the control into memory.
Set your properties
Call .loadIt()
load the control into a placeholder on the page

This way, you gain control over what's firing when. I'm unclear as to how the events fire when I load them programatically, so I always take the approach I've mentioned above if the controls are working off of parent page properties, instead of global config type stuff from the web.config, in which case I'll stick with page_load

If you still can't get it to work, post up the code, and let's have a look. I love this design pattern, and so I think it would be great if you could get ahold of it, too.

paul
penny1.gif
penny1.gif
 
Thank you. I finally got it to work!! Your help was great and I often check out your posts.

I was side-tracked on Wednesday and was able to move on something else for a bit....so the problem just lay there. On Thursday I was at a differnet client where there system utilizes the &quot;old school asp&quot; and not asp.net.

I am back in on Friday and was able to clean it all in one shot.

Yes, I noticed the way the code fired and found it difficult. I did see one your previous posts regarding the LoadID() public sub, but totally forgot to try that method!!

again, thanks for your help. You must be an instructor or have done instructional work before. You're very helpful.

And this UC stuff is COOL!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top