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 13th, 2004, 04:44 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Yes, static will do as you say, however, I'm confused by your use of it in the statement you made. In the case of a static value in a user control class, all instances of the user control will share the value, so if the page has 3 instances of the user control on it, they will each have the same value if one changes it. However, you stated "so all pages can use it". This will not be the case. Different pages are complete different executions so they can never "share" data in anything apart from using the ASP.NET mechanisms for sharing data (the session, cookies, etc.).

Now about the DDL...
I'm assuming you mean that you have DDL that is your season chooser. You want the change action on this DDL to serve are the notifying event... so you need to do something like this (please excuse the C# if it's wrong. I don't know it cold):

public event EventHandler SeasonChanged;
protected DropDownList ddlSeason;
private void ddlSeason_onSelectedIndexChanged()
{
    SeasonChanged(this, new System.EventArgs());
}

Now when the DDL is changed, the event will be raised by the DDL's event handler. This goes back to that event bubbling example I posted a while ago.
 
Old July 14th, 2004, 08:42 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I found this article that I'm following - http://www.odetocode.com/code/94.aspx

I added this to the User Control:
private void InitializeComponent()
{
    this.Load += new System.EventHandler(this.Page_Load);
    this.upDateDates += new EventHandler(dates_upDateDates);
}
Then this:
protected void dates_upDateDates(object sender, System.EventArgs e)
{
    OnupDateDates(e);
}
public event EventHandler upDateDates;
protected void OnUpDateDates(EventArgs e)
{
    if(upDateDates != null)
    {
        upDateDates(this, e);
    }
}

the DDL is looking for dates_upDateDates.

Then on the Parent Page:
protected asmain.controls.dates Dates1; - declared up top

private void InitializeComponent()
{
    this.Load += new System.EventHandler(this.Page_Load);
    Dates1.upDateDates += new EventHandler(Dates1_upDateDates);
}
private void Dates1_upDateDates(object sender, EventArgs e)
{
    Response.Write("i made it to the parent page");
}

I'm getting "Object reference not set to an instance of an object." on Dates1.upDateDates += new EventHandler(Dates1_upDateDates);



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

You have the user control on the page in markup? That's the only thing I can come up with.
 
Old July 14th, 2004, 09:09 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I'm loading the control dynamically based on a querystring value.

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

I dropped the user control on the page and got a StackOverflowException and then put a try catch statement in the User Control here:
protected void dates_upDateDates(object sender, System.EventArgs e)
        {
            try
            {
                OnUpDateDates(e);
            }
            catch(System.StackOverflowException ex)
            {
                Response.Write(ex.Message);
            }
}

It printed out "i made it to the parent page" 100+ times...
 
Old July 14th, 2004, 10:37 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Ok, well if you are loading the control ONLY in the codebehind then you need an instance of it:

No instance:
protected asmain.controls.dates Dates1;

Instance:
protected asmain.controls.dates Dates1 = new asmain.controls.dates();

I'm not sure what the other problem is when you put the control on the page. That almost sounds like you are instantiating the control from within itself which is causing the stack overflow.
 
Old July 19th, 2004, 03:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I got the Parent to recognize the event. But now how do I pass the results of the new event handler back to the User Control's Literal Control?
 
Old July 20th, 2004, 09:08 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You need to expose properties to do these sorts of things.

You would need a public property on the user control class that exposes the desired property of the literal control:

public string myLiteralControlText
{
    get
    {
        return myLiteralControl.Text;
    }
    set
    {
        myLiteralControl.Text = value;
    }
}

Also, for the value of your drop down list, you'll probably want something like this:

public string DDLSelectValue
{
    get
    {
        return myDDL.SelectValue;
    }
}

Why are you having the parent handle an event that is essentially doing something internal? It sounds to me like you are raising an event from your user control, then having the parent handle it and set a user control's control property to a value that you are getting from the user control itself.
 
Old July 20th, 2004, 09:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Quote:
quote:
Why are you having the parent handle an event that is essentially doing something internal? It sounds to me like you are raising an event from your user control, then having the parent handle it and set a user control's control property to a value that you are getting from the user control itself.
Yep :)

I chose one way and stuck with it when I couldn't figure it out but now I want to learn the right way so if I need to do it again I can go back and look at my example. Plus it's a great learning experience.

So, if in my senario I need to bubble an event from the DDL up to the Parent and never send anything back to the User Control?

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

I went back and let the user control handle the main event but still passed what I needed to the parent and it worked. I didn't know you could do that. It works now.

PLanoie, thanks for your help.






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.