picking up value in code behind

Discussion in 'ASP .Net' started by Scott Collens, Aug 8, 2003.

  1. Hi there, does anyone know if it is possible to set a value in a user
    control and pick it up in the code behind.

    I'm trying to reuse the same user control and code behind for multiple
    pages but I don't want to use a querystring param. Thanks for
    listening...Scott
     
    Scott Collens, Aug 8, 2003
    #1
    1. Advertising

  2. "Scott Collens" <> wrote in message news:Xns93D0D1997B0E8scottcollensnospamho@207.46.248.16...
    > Hi there, does anyone know if it is possible to set a value in a user
    > control and pick it up in the code behind.
    >


    Scott,
    Create a public Property for the User Control. Then in the WebForm's code-behind,
    call the User Control's Property to get the value.

    --

    Thanks,
    Carl Prothman
    Microsoft ASP.NET MVP
    http://www.able-consulting.com
     
    Carl Prothman [MVP], Aug 8, 2003
    #2
    1. Advertising

  3. "Scott Collens" <> wrote
    > "Carl Prothman [MVP]" <> wrote
    > > "Scott Collens" <> wrote
    > > > Hi there, does anyone know if it is possible to set a value in a user
    > > > control and pick it up in the code behind.

    > >
    > > Create a public Property for the User Control. Then in the WebForm's
    > > code-behind, call the User Control's Property to get the value.

    >
    > Thanks for the response, but I'm a little confused as to how this is picked
    > up in the code behind. Can you provide an example? Thanks again....Scott
    >


    Scott,
    In the User Control, create a public property

    public string Username
    {
    get
    {
    return TextBox1.Text;
    }
    set
    {
    TextBox1.Text = value;
    }
    }

    Then in the code behind of the WebForm (which contains the User Control)
    create a protected member variable for the User Control. In this case,
    the C# project is called "myProject" and the User Control is called
    "UserControlPropertyDemo"

    protected myProject.UserControlPropertyDemo UserControlPropertyDemo1;

    Then in some event handler, get or set the User Control's public property value

    // Set the property value
    UserControlPropertyDemo1.Username = "CarlProthman";

    // Get the property value
    string username = UserControlPropertyDemo1.Username;

    --

    Thanks,
    Carl Prothman
    Microsoft ASP.NET MVP
    http://www.able-consulting.com
     
    Carl Prothman [MVP], Aug 8, 2003
    #3
  4. Thanks Carl, I'll give it a try.


    "Carl Prothman [MVP]" <> wrote in
    news::

    > "Scott Collens" <> wrote
    >> "Carl Prothman [MVP]" <> wrote
    >> > "Scott Collens" <> wrote
    >> > > Hi there, does anyone know if it is possible to set a value in a
    >> > > user control and pick it up in the code behind.
    >> >
    >> > Create a public Property for the User Control. Then in the
    >> > WebForm's code-behind, call the User Control's Property to get the
    >> > value.

    >>
    >> Thanks for the response, but I'm a little confused as to how this is
    >> picked up in the code behind. Can you provide an example? Thanks
    >> again....Scott
    >>

    >
    > Scott,
    > In the User Control, create a public property
    >
    > public string Username
    > {
    > get
    > {
    > return TextBox1.Text;
    > }
    > set
    > {
    > TextBox1.Text = value;
    > }
    > }
    >
    > Then in the code behind of the WebForm (which contains the User
    > Control) create a protected member variable for the User Control. In
    > this case, the C# project is called "myProject" and the User Control
    > is called "UserControlPropertyDemo"
    >
    > protected myProject.UserControlPropertyDemo
    > UserControlPropertyDemo1;
    >
    > Then in some event handler, get or set the User Control's public
    > property value
    >
    > // Set the property value
    > UserControlPropertyDemo1.Username = "CarlProthman";
    >
    > // Get the property value
    > string username = UserControlPropertyDemo1.Username;
    >
    > --
    >
    > Thanks,
    > Carl Prothman
    > Microsoft ASP.NET MVP
    > http://www.able-consulting.com
    >
    >
    >
    >
    >
     
    Scott Collens, Aug 13, 2003
    #4
    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. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    652
    Alphonse Giambrone
    Jun 28, 2003
  2. Replies:
    2
    Views:
    1,097
    Henry S. Thompson
    Mar 6, 2006
  3. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    534
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
  4. Jonathan
    Replies:
    2
    Views:
    442
    Steven Cheng
    Mar 11, 2008
  5. anyeone
    Replies:
    6
    Views:
    247
    Phillip Williams
    Dec 21, 2005
Loading...

Share This Page