Properties for User Controls loaded Programmatically

Discussion in 'ASP .Net Building Controls' started by TK, Feb 24, 2004.

  1. TK

    TK Guest

    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
     
    TK, Feb 24, 2004
    #1
    1. Advertising

  2. 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




    "TK" <> wrote in message
    news:...
    > 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
     
    Alessandro Zifiglio, Feb 26, 2004
    #2
    1. Advertising

  3. 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




    "Alessandro Zifiglio" <> wrote in
    message news:npm%b.11906$...
    > 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
    >
    >
    >
    >
    > "TK" <> wrote in message
    > news:...
    > > 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

    >
    >
     
    Alessandro Zifiglio, Feb 26, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Smith
    Replies:
    1
    Views:
    507
    Martin Dechev
    Oct 11, 2004
  2. Guest
    Replies:
    4
    Views:
    7,173
    Steven Cheng[MSFT]
    Nov 9, 2005
  3. ABC
    Replies:
    0
    Views:
    484
  4. Joey
    Replies:
    0
    Views:
    707
  5. Newbie

    Programmatically Get to User Control Properties

    Newbie, Jul 13, 2005, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    125
    Newbie
    Jul 14, 2005
Loading...

Share This Page