473,378 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

User Control and member variables

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
Nov 19 '05 #1
1 1898
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" <jo********@comcast.net> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
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

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris Millar | last post by:
I have a user control that i wish to extend to change the date when the user selects the numeric up down button. The code explains itself, hope someone can help. any ideas appreaciated.. ...
3
by: Christopher Young | last post by:
I have several user controls on a page and I am trying to get information out of them. The postback is being caused on the aspx page and not in the user control. I have tried using a property but...
6
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
8
by: Alan Silver | last post by:
Hello, I'm trying to write a simple user control as an exercise (and 'cos it might be useful). I am trying to do a simple date picker, which consists of three drop down lists, one for the day,...
2
by: Alan Silver | last post by:
Hello, I'm having rather a problem with user control. It is a fairly simple affair (see my other threads for more details) that shows a date and time in five drop down controls. I had private...
2
by: JohnK | last post by:
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...
0
by: ben | last post by:
I am having some difficulty in understanding how to deal with variables, session states, objects, user controls while attempting to develop a ASP.Net application. I have created several user...
2
by: Mark | last post by:
Hi. I am making a user control right now, and it looks something like this: <script runat="server"> public string SelectCommand { set { // see below for why the following line is here.
9
by: David | last post by:
With a non-server app there is one instance of the program running and one user 'using' it at a time. With this scenario I'm pretty comfortable with variable scope and lifetime. With a server app...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.