 |
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

July 12th, 2004, 08:09 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
User Control
I have a asp:label outside a user control. Is there a way to modify the label in the user control's code behind?
|

July 12th, 2004, 08:43 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Not directly. You have several options:
Ok (but icky) - In the user control, you look for a control by the known name in the control's parent's controls collection (Me.Parent.Controls...). HOWEVER: this forces the control to make some assumptions about who its parent is and the control it's going to affect. You should check for existance of the parent's control (using FindControl and a null check) before you attempt to set its value. If you ever want to change the label to another control, you'll have to change the user control.
Better - In the user control, you provide a public property of type Label. Then in the page's code behind you set that property to the instance of the label on the page. However, this also makes the assumption that whatever you are using this for is always going to be a label.
Best - Create an event on the user control to notify that something has happened. (I'm assuming that you are setting the label text based on something happening in the user control.) Then you can either pass the text within the event args, or expose a public property on the user control to access this text. The consuming page can then handle the event.
The third is well, the best solution because it isolates the control from the parent such that the user control makes no assumptions about what the other control it will affect is. This method also follows the event driven structure of the .NET controls. Granted, the third method requires the most code, but you get what you pay for.
|

July 12th, 2004, 10:44 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
When you say expose a public property do you mean like this?
declared up top: public string _season;
public string mySeason
{
get
{
return _season;
}
set
{
_season = value;
}
}
|

July 12th, 2004, 12:15 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Exactly.
|

July 12th, 2004, 12:52 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
When you say "The consuming page can then handle the event." do you mean put the event in the main page or leave it on the user control?
|

July 12th, 2004, 02:56 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Here is some of the code I'm playing with:
I changed the property and the variable to static so all pages can use it.
On the parent page I use <code>lblSeason.Text = asmain.controls.dates.mySeason;</code> but it's always one click behind. I've never used property(s) before so I'm clueless as to how they work.
|

July 12th, 2004, 07:15 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
User Control:
Public Event SomethingHappened()
(Call "RaiseEvent SomethingHappened" when something happens inside the user control that the user control's consumer [i.e. the page] should know about.)
Page:
Private Sub MyControl_SomethingHappened() Handles MyControl.SomethingHappened
'Do what you need on the page
lblSeason.Text = asmain.controls.dates.mySeason
End Sub
This behavior is identical to other server control event behavior. You are experiencing a one-click delay most likely because you are calling that property assignment in the page load. However, when your user control changes it's property "MySeason" you are already passed page_load on the page, so you end up with the old value. That's the reason for using an event. The user control raises the event and the page then handles that event as soon as it happens, regardless of what happened in page_load.
|

July 12th, 2004, 07:17 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Why did you make a property and variable static? I think you might be missing the point of "static".
(Oh, and sorry for my vb code samples. It's second nature.)
|

July 13th, 2004, 10:45 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
I just started learning C# last week and read that static is shared by all instances of the class.
|

July 13th, 2004, 02:09 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
I'm getting this error message:
An object reference is required for the nonstatic field, method, or property 'asmain.controls.dates.mySeason'
I need my hand held on this one because I'm completely lost...
UserControl:
public event EventHandler upDateDates;
protected void OnupDateDates2(EventArgs e)
{
if(upDateDates != null)
{
upDateDates(this, e);
}
}
ParentPage:
private void lblSeason_upDateDates(object sender, System.EventArgs e)
{
//my event code here plus...
lblSeason.Text = asmain.controls.dates.mySeason;
}
Now my question is in the asp:dropdownlist(UserControl) do I need to change the OnSelectedIndexChanged to reflect the ParentPage or keep it on the UserControl's event?
|
|
 |