Wrox Programmer Forums
|
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
 
Old July 12th, 2004, 08:09 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default 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?

 
Old July 12th, 2004, 08:43 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.
 
Old July 12th, 2004, 10:44 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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;
    }
}

 
Old July 12th, 2004, 12:15 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Exactly.
 
Old July 12th, 2004, 12:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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?

 
Old July 12th, 2004, 02:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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.

 
Old July 12th, 2004, 07:15 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.
 
Old July 12th, 2004, 07:17 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.)
 
Old July 13th, 2004, 10:45 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I just started learning C# last week and read that static is shared by all instances of the class.

 
Old July 13th, 2004, 02:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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?







Similar Threads
Thread Thread Starter Forum Replies Last Post
user control in user control prijatelce ASP.NET 2.0 Basics 2 May 2nd, 2008 08:42 AM
Add Windows User control in Web User Control agarwalvidhu C# 0 March 30th, 2006 01:17 AM
Help! Custom Server Control using User Control diehard ASP.NET 1.0 and 1.1 Professional 2 January 4th, 2006 12:33 PM
Help with control initialization in user control mike_remember ASP.NET 1.0 and 1.1 Professional 7 December 19th, 2005 11:08 AM
user control nashnash ASP.NET 1.0 and 1.1 Professional 6 November 8th, 2004 09:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.