User Control and member variables

Discussion in 'ASP .Net' started by John Keenan, Aug 19, 2005.

  1. John Keenan

    John Keenan Guest

    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
    John Keenan, Aug 19, 2005
    #1
    1. Advertising

  2. John Keenan

    Karl Seguin Guest

    What's happening makes total sense to me. I'm not sure how you think it
    should ever say LinkButton1_Click or LinkButton2_Click

    Walk through this with me...

    Initial Load -->
    Page_Load (Host)
    Page_Load (User Control) --> Property set to "this is from PAge_Load"

    Click On user control LinkButton2 -->
    Page_Load (Host)
    Page_Load (User Control) --> Property set to "this is from PAge_Load"
    LinkButton2_Click (User Control) property set to "LinkButton2"

    Click On host Button1-->
    Page_Load (Host)
    Page_Load (User Control) --> Property set to "this is from PAge_Load"
    Button1_Click (Host) --> display property, which was set above to
    "Page_Load"


    Perhaps in your mind you are thinking that having set property to
    "LinkButton2" on the 2nd case, this is what should be shown? If so, there
    are 2 things playing against you
    (a) User control's page_load is firing and resetting the value to
    "Page_Load"
    (b) The property isn't preserved between trips...


    so to fix (a), we need to change it to:
    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!Page.IsPostBack){
    mUC1Label= "this is from Page_Load";
    }
    }


    and to fix (b), we need to somehow persist the value across postback by
    storing it somewhere (the viewstate is a good place), remove the field
    mUC1Label and change the property to (wherever you use the field, also use
    the property):

    public string UC1LinkButton
    {
    get { return (string)ViewState["uc1Label"]; }
    set {
    ViewState["uc1Label"] = value;
    uc1Label1.Text = value;
    }
    }




    --
    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!)


    "John Keenan" <> wrote in message
    news:O$...
    >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
    #2
    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. Mad Scientist Jr
    Replies:
    0
    Views:
    625
    Mad Scientist Jr
    Mar 22, 2006
  2. dwok
    Replies:
    7
    Views:
    685
    Hal Rosser
    Mar 4, 2005
  3. Replies:
    9
    Views:
    927
  4. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    530
    Jonathan Wood
    Feb 2, 2010
  5. K. Frank
    Replies:
    20
    Views:
    378
Loading...

Share This Page