User Control & member vars

Discussion in 'ASP .Net' started by =?Utf-8?B?Sm9obks=?=, Aug 19, 2005.

  1. I have a user control with 2 buttons on it & 1 label.... as each button is
    pressed, they set a member variable within the class and sets the label
    test.
    I also have a get/set property for the member variable.
    At Page_Load time I initialize this member variable

    I have a host form which contains the user control
    I have a button and a label on the host form (in addition to the user
    control)
    Now when I click the host-form button, it is suppose to get the property
    from the user control and put the contents
    into the host-form label.....

    the following is from the user control:
    private void Page_Load(object sender, System.EventArgs e)
    {
    mUC1Label= "this is from Page_Load";
    }
    public string UC1LinkButton
    {
    get { return mUC1Label; }
    set {
    mUC1Label = value;
    uc1Label1.Text = mUC1Label;
    }
    }
    private void LinkButton1_Click(object sender, System.EventArgs e)
    {
    UC1LinkButton = "LinkButton1_Click";
    }
    private void LinkButton2_Click(object sender, System.EventArgs e)
    {
    UC1LinkButton = "LinkButton2_Click";
    }

    on the host side:
    void Button1_Click(object sender, System.EventArgs e)
    {
    ucTestLabel1.Text = "from the User Control Below " +
    Webusercontrol12.UC1LinkButton;
    }

    ucTestLabel1 is a host-form control
    now Button1_Click is not in the Code-Behind... it is in the form page (if
    that makes a difference)

    what shows up in ucTestLabel1 always is "this is from Page_Load" ,
    never "LinkButton1_Click"; or "LinkButton2_Click";

    why?

    John
    =?Utf-8?B?Sm9obks=?=, Aug 19, 2005
    #1
    1. Advertising

  2. The property in the User Control should store in view state to persist across
    postbacks

    public string UC1LinkButton
    {
    get {
    if ( ViewState["UC1Text"]!=null)
    return (string)ViewState["UC1Text"];
    else
    return string.Empty;
    }
    set {
    ViewState["UC1Text"]=value;
    }
    }


    "JohnK" wrote:

    > I have a user control with 2 buttons on it & 1 label.... as each button is
    > pressed, they set a member variable within the class and sets the label
    > test.
    > I also have a get/set property for the member variable.
    > At Page_Load time I initialize this member variable
    >
    > I have a host form which contains the user control
    > I have a button and a label on the host form (in addition to the user
    > control)
    > Now when I click the host-form button, it is suppose to get the property
    > from the user control and put the contents
    > into the host-form label.....
    >
    > the following is from the user control:
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > mUC1Label= "this is from Page_Load";
    > }
    > public string UC1LinkButton
    > {
    > get { return mUC1Label; }
    > set {
    > mUC1Label = value;
    > uc1Label1.Text = mUC1Label;
    > }
    > }
    > private void LinkButton1_Click(object sender, System.EventArgs e)
    > {
    > UC1LinkButton = "LinkButton1_Click";
    > }
    > private void LinkButton2_Click(object sender, System.EventArgs e)
    > {
    > UC1LinkButton = "LinkButton2_Click";
    > }
    >
    > on the host side:
    > void Button1_Click(object sender, System.EventArgs e)
    > {
    > ucTestLabel1.Text = "from the User Control Below " +
    > Webusercontrol12.UC1LinkButton;
    > }
    >
    > ucTestLabel1 is a host-form control
    > now Button1_Click is not in the Code-Behind... it is in the form page (if
    > that makes a difference)
    >
    > what shows up in ucTestLabel1 always is "this is from Page_Load" ,
    > never "LinkButton1_Click"; or "LinkButton2_Click";
    >
    > why?
    >
    > John
    >
    >
    =?Utf-8?B?RGhhbmFuamF5YQ==?=, Aug 19, 2005
    #2
    1. Advertising

  3. =?Utf-8?B?Sm9obks=?=

    Karl Seguin Guest

    this was double posted, and answered in the other post...take a look.

    karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is
    annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)
    "JohnK" <> wrote in message
    news:...
    >I have a user control with 2 buttons on it & 1 label.... as each button is
    > pressed, they set a member variable within the class and sets the label
    > test.
    > I also have a get/set property for the member variable.
    > At Page_Load time I initialize this member variable
    >
    > I have a host form which contains the user control
    > I have a button and a label on the host form (in addition to the user
    > control)
    > Now when I click the host-form button, it is suppose to get the property
    > from the user control and put the contents
    > into the host-form label.....
    >
    > the following is from the user control:
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > mUC1Label= "this is from Page_Load";
    > }
    > public string UC1LinkButton
    > {
    > get { return mUC1Label; }
    > set {
    > mUC1Label = value;
    > uc1Label1.Text = mUC1Label;
    > }
    > }
    > private void LinkButton1_Click(object sender, System.EventArgs e)
    > {
    > UC1LinkButton = "LinkButton1_Click";
    > }
    > private void LinkButton2_Click(object sender, System.EventArgs e)
    > {
    > UC1LinkButton = "LinkButton2_Click";
    > }
    >
    > on the host side:
    > void Button1_Click(object sender, System.EventArgs e)
    > {
    > ucTestLabel1.Text = "from the User Control Below " +
    > Webusercontrol12.UC1LinkButton;
    > }
    >
    > ucTestLabel1 is a host-form control
    > now Button1_Click is not in the Code-Behind... it is in the form page (if
    > that makes a difference)
    >
    > what shows up in ucTestLabel1 always is "this is from Page_Load" ,
    > never "LinkButton1_Click"; or "LinkButton2_Click";
    >
    > why?
    >
    > John
    >
    >
    Karl Seguin, Aug 19, 2005
    #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. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    381
  2. Alex Hunsley
    Replies:
    4
    Views:
    338
    Sudsy
    Oct 27, 2003
  3. Martin T.

    Static Vars in member-functions?

    Martin T., Mar 7, 2005, in forum: C++
    Replies:
    2
    Views:
    298
    Martin T.
    Mar 7, 2005
  4. Linuxguy123
    Replies:
    7
    Views:
    670
    Paddy O'Loughlin
    Feb 20, 2009
  5. caccolangrifata
    Replies:
    18
    Views:
    385
    Chris Torek
    Jul 22, 2011
Loading...

Share This Page