How do you pass an ascx user control a variable??? Help please

Discussion in 'ASP .Net' started by simon, Sep 7, 2006.

  1. simon

    simon Guest

    hello. relatively new to vb.net, i'm using VS 2003 and .net 2.0
    i have a web app that i'm i have a user control that displays a simple
    1 row table as the header of the page. the user control takes one
    input variable, the string that will be displayed as the title of the
    page. example call:
    <uc1:Header runat=server ID="ucHeader" Title="Home Page" />

    what i am looking to do on a certain page is have the "title" value be
    dynamic, in that i will pull the string from the database and pass
    that to the user control.
    i have the process working just fine if i'm passing it into a label,
    but the user control is not receiving the setting of this value. i'm
    assuming that controls on a page are process first before the page.

    how would i do this? example code would be very much appreciated!!
    thank you.
     
    simon, Sep 7, 2006
    #1
    1. Advertising

  2. Hi Simon,
    You need to specify a property in your control as well as a variable to
    hold the value. For example:
    Dim _Title As String
    Property Title() As String
    Get
    Return _Title
    End Get
    Set(ByVal value As String)
    _Title = value
    End Set
    End Property

    Then, from within your control you can use _Title just like any other
    variable. You can also reference this in your page code by doing
    mycontrol.Title = "something" assuming that you add the appropriate
    mycontrol declaration.

    Thanks,
    Matt

    "simon" <> wrote in message
    news:...
    > hello. relatively new to vb.net, i'm using VS 2003 and .net 2.0
    > i have a web app that i'm i have a user control that displays a simple
    > 1 row table as the header of the page. the user control takes one
    > input variable, the string that will be displayed as the title of the
    > page. example call:
    > <uc1:Header runat=server ID="ucHeader" Title="Home Page" />
    >
    > what i am looking to do on a certain page is have the "title" value be
    > dynamic, in that i will pull the string from the database and pass
    > that to the user control.
    > i have the process working just fine if i'm passing it into a label,
    > but the user control is not receiving the setting of this value. i'm
    > assuming that controls on a page are process first before the page.
    >
    > how would i do this? example code would be very much appreciated!!
    > thank you.
    >
    >
     
    Matt MacDonald, Sep 7, 2006
    #2
    1. Advertising

  3. simon

    Guest

    Re: How do you pass an ascx user control a variable??? Help please

    I would use session variables. Easy quick and affective. set the
    session in the main page like, Session["Title"] = "My New Title" then
    in the control have the control look for Session["Title]" != null then
    set the title to the session variable

    simon wrote:
    > hello. relatively new to vb.net, i'm using VS 2003 and .net 2.0
    > i have a web app that i'm i have a user control that displays a simple
    > 1 row table as the header of the page. the user control takes one
    > input variable, the string that will be displayed as the title of the
    > page. example call:
    > <uc1:Header runat=server ID="ucHeader" Title="Home Page" />
    >
    > what i am looking to do on a certain page is have the "title" value be
    > dynamic, in that i will pull the string from the database and pass
    > that to the user control.
    > i have the process working just fine if i'm passing it into a label,
    > but the user control is not receiving the setting of this value. i'm
    > assuming that controls on a page are process first before the page.
    >
    > how would i do this? example code would be very much appreciated!!
    > thank you.
     
    , Sep 7, 2006
    #3
  4. simon

    simon Guest

    hello matt,
    thanks for the reply. what you described at the end ...
    mycontrol.Title = "something"
    is exactly what i was looking to do.
    where would the property definition go? is that in the ascx code
    behind?
    also, once this is done, would the normal way of calling the control
    still work? like having a title hard coded in the call (as they are
    now)
    thanks again

    On Thu, 7 Sep 2006 11:24:59 -0400, "Matt MacDonald"
    <> wrote:

    >Hi Simon,
    > You need to specify a property in your control as well as a variable to
    >hold the value. For example:
    >Dim _Title As String
    >Property Title() As String
    > Get
    > Return _Title
    > End Get
    > Set(ByVal value As String)
    > _Title = value
    > End Set
    >End Property
    >
    >Then, from within your control you can use _Title just like any other
    >variable. You can also reference this in your page code by doing
    >mycontrol.Title = "something" assuming that you add the appropriate
    >mycontrol declaration.
    >
    >Thanks,
    > Matt
    >
    >"simon" <> wrote in message
    >news:...
    >> hello. relatively new to vb.net, i'm using VS 2003 and .net 2.0
    >> i have a web app that i'm i have a user control that displays a simple
    >> 1 row table as the header of the page. the user control takes one
    >> input variable, the string that will be displayed as the title of the
    >> page. example call:
    >> <uc1:Header runat=server ID="ucHeader" Title="Home Page" />
    >>
    >> what i am looking to do on a certain page is have the "title" value be
    >> dynamic, in that i will pull the string from the database and pass
    >> that to the user control.
    >> i have the process working just fine if i'm passing it into a label,
    >> but the user control is not receiving the setting of this value. i'm
    >> assuming that controls on a page are process first before the page.
    >>
    >> how would i do this? example code would be very much appreciated!!
    >> thank you.
    >>
    >>

    >
     
    simon, Sep 7, 2006
    #4
  5. The property definition would need to go in the codebehind for the control.

    As far as calling the control, you would need to drag it into your page html
    like you said in your first post:
    <uc1:Header runat=server ID="ucHeader" Title="Home Page" />

    In VS2003 you need to add the control declaration to the codebehind of your
    page as well in order to be able to reference it:
    Protected WithEvents ucHeader as Header <-- This would go in the code behind
    up where the rest of the control definitions are.

    See if that works for you.

    Thanks,
    Matt

    "simon" <> wrote in message
    news:...
    > hello matt,
    > thanks for the reply. what you described at the end ...
    > mycontrol.Title = "something"
    > is exactly what i was looking to do.
    > where would the property definition go? is that in the ascx code
    > behind?
    > also, once this is done, would the normal way of calling the control
    > still work? like having a title hard coded in the call (as they are
    > now)
    > thanks again
    >
    > On Thu, 7 Sep 2006 11:24:59 -0400, "Matt MacDonald"
    > <> wrote:
    >
    >>Hi Simon,
    >> You need to specify a property in your control as well as a variable to
    >>hold the value. For example:
    >>Dim _Title As String
    >>Property Title() As String
    >> Get
    >> Return _Title
    >> End Get
    >> Set(ByVal value As String)
    >> _Title = value
    >> End Set
    >>End Property
    >>
    >>Then, from within your control you can use _Title just like any other
    >>variable. You can also reference this in your page code by doing
    >>mycontrol.Title = "something" assuming that you add the appropriate
    >>mycontrol declaration.
    >>
    >>Thanks,
    >> Matt
    >>
    >>"simon" <> wrote in message
    >>news:...
    >>> hello. relatively new to vb.net, i'm using VS 2003 and .net 2.0
    >>> i have a web app that i'm i have a user control that displays a simple
    >>> 1 row table as the header of the page. the user control takes one
    >>> input variable, the string that will be displayed as the title of the
    >>> page. example call:
    >>> <uc1:Header runat=server ID="ucHeader" Title="Home Page" />
    >>>
    >>> what i am looking to do on a certain page is have the "title" value be
    >>> dynamic, in that i will pull the string from the database and pass
    >>> that to the user control.
    >>> i have the process working just fine if i'm passing it into a label,
    >>> but the user control is not receiving the setting of this value. i'm
    >>> assuming that controls on a page are process first before the page.
    >>>
    >>> how would i do this? example code would be very much appreciated!!
    >>> thank you.
    >>>
    >>>

    >>

    >
     
    Matt MacDonald, Sep 8, 2006
    #5
    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. Quentin
    Replies:
    1
    Views:
    489
    Quentin
    Jul 29, 2003
  2. Jason Shohet
    Replies:
    1
    Views:
    485
    Kevin Spencer
    Nov 10, 2003
  3. Holger (David) Wagner
    Replies:
    2
    Views:
    1,945
    Holger (David) Wagner
    Jul 3, 2004
  4. Joe
    Replies:
    1
    Views:
    375
    Jeffrey Tan[MSFT]
    Feb 16, 2004
  5. Replies:
    2
    Views:
    539
Loading...

Share This Page