Properties for User Controls loaded Programmatically

T

TK

I have x number of user controls that have a strong type (e.g.
className uc1). I am able to access their properties through a single
aspx page using:

uc = LoadControl(path to control based on querystring value)
textbox.Text = CType(uc, uc1).someproperty

The thing I need to do though is to specify the type (e.g. uc1) on the
fly based on the path created for that user control. For example
(ignoring declarations):

path = "path to user control directory"

'For each valid ucid there is a strong type associated with the
'user control using className in the ascx page.
ucid = request.querystring("ucid")

uc = LoadControl(path & ucid)

'Add the user control to the page
placeholder.Controls.Add(uc)

This is where I am stuck...
I need to access the properties of the control based on the ucid.
When I hard code it I use CType and specify the user control
className.
But in this case I do not know which user control will be called as it
depends on the ucid (all the user controls have the same properties).
There may 20-30 user controls and I would hate to have to hard code
each of the possibilities.

Ultimately I am looking for something like this:

path = "path to user control directory"
ucid = request.querystring("ucid")
uc = LoadControl(path & ucid)
placeholder.Controls.Add(uc)
textbox.text = uc.someproperty


Thx for any help or direction!

Tom
 
A

Alessandro Zifiglio

TK, are you using vs.net or are you coding this in a tool like web matrix or
just plain note pad. Thing is, if your using web matrix or note pad, the
approaches these tools use is spaghetti code that is a combination of the
presentation with code. Vs.net makes it easier to seperate your code from
your presentation by using a concept which you might have already heard of
"Codebehind" classes. The reason i'm telling you this is because some of the
sample code on msdn/gotdotnet/docs assume you are not using vs.net for your
development and provide you with sample as to how to achieve your goals
using the spaghetti code approach. This is why on the example code you are
seeing it reads "'For each valid ucid there is a strong type associated with
the user control using className in the ascx page." and if you are coding
using code behind classes then you do not have to bother with this --clarify
this for me and what it is you want to do i'll provide you with the help you
need ;P
 
A

Alessandro Zifiglio

I got stuck on the line where you stated "'For each valid ucid there is a
strong type associated with the user control using className in the ascx
page" doh! Now that i re-read your post, i think, i get where your getting
at. You will have to use a lot of if statements and conditionally check if
its of that specific type and then perform your needs. Look at the sample
code below :

uc = LoadControl(path to control based on querystring value)

If TypeOf uc Is UC1 Then
textbox.Text = CType(uc, uc1).someproperty
elseif TypeOf uc Is UC2 Then
textbox.Text = CType(uc, uc2).someproperty
'more conditions ....
End if
....etc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,266
Latest member
DavidaAlla

Latest Threads

Top