handling events raised in user controls
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngescalate' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Jesse Liberty (VIP)
This message was not answered on Controls. I've added it here because it is
now the end of the day and this question is pretty urgent for me...

I am having difficulty with handling events raised in user controls.

I have a user control in a file named booklist.cs. I also have a web page
which has an instance of this user control. Within the booklist user control
is a drop down list.

When the user clicks on an entry in the drop down list, an event is fired
and caught in the user control.The response of the user control is to raise
its own event: listchanged.

In order to respond to the listChanged event fired by the user control, I
must declare an instance of the user control in the web page. If I declare
it as follows:

UserControl1.Booklist Booklist

The page does not recognize that this is the same booklist that was declared
in the aspx page, and Booklist remains null. If I declare it like this:

System.Web.UI.UserControl Booklist;

the page DOES recognize it, it is not null and it corresponds to the right
object. The problem is that I can then not set up the event handler:

this.Booklist.ListChanged +&# n;ew
UserControl1.BookList.ListChangedHandler(this.Booklist_Listchanged);

The error is that the class Web.UI.UserControl does not have a ListChanged
event. My solution is to create two instance variables...

System.Web.UI.UserControl Booklist; // matches the declaration in the
aspx file
UserControl1.Booklist bklst; // matches the declaration in the ascx
file

then in my initialize component method I cast one to the other...

bklst &# (;UserControl1.Booklist) Booklist; // cast
this.bklst.ListChanged +&# n;ew
UserControl1.BookList.ListChangedHandler(this.Booklist_Listchanged);

This seems cumbersome. Am I missing something obvious?

Also a few more questions: I have a user control called booklist and a web
form. In the user control is a list box. I know that events from that list
box must be caught in the user control. Am I right that there is no way to
let them percolate up to the enclosing page?

Thanks.
Reply to this message...
 
    
Jesse Liberty (VIP)
This appears to have been a compiler bug. I rewrote the example and this
time I'm able to declare my user control properly and VS.NET is hooking it
up properly:
protected UserControl1.BookList Booklist;

this.Booklist.ListChanged +&# n;ew
UserControl1.BookList.ListChangedHandler(this.Booklist_ListChanged);

Thanks all.

-j

-------------------------------
Jesse Liberty
Liberty Associates, Inc.
.NET Training & Development
http://www.LibertyAssociates.com

-----Original Message-----
From: Jesse Liberty [mailto:Click here to reveal e-mail address]
Sent: Monday, September 10, 2001 5:27 PM
To: aspngescalate
Subject: [aspngescalate] handling events raised in user controls

This message was not answered on Controls. I've added it here because it
is now the end of the day and this question is pretty urgent for me...

I am having difficulty with handling events raised in user controls.

I have a user control in a file named booklist.cs. I also have a web page
which has an instance of this user control. Within the booklist user control
is a drop down list.

When the user clicks on an entry in the drop down list, an event is fired
and caught in the user control.The response of the user control is to raise
its own event: listchanged.

In order to respond to the listChanged event fired by the user control, I
must declare an instance of the user control in the web page. If I declare
it as follows:

UserControl1.Booklist Booklist

The page does not recognize that this is the same booklist that was
declared in the aspx page, and Booklist remains null. If I declare it like
this:

System.Web.UI.UserControl Booklist;

the page DOES recognize it, it is not null and it corresponds to the right
object. The problem is that I can then not set up the event handler:

this.Booklist.ListChanged +&# n;ew
UserControl1.BookList.ListChangedHandler(this.Booklist_Listchanged);

The error is that the class Web.UI.UserControl does not have a ListChanged
event. My solution is to create two instance variables...

System.Web.UI.UserControl Booklist; // matches the declaration in the
aspx file
UserControl1.Booklist bklst; // matches the declaration in the ascx
file

then in my initialize component method I cast one to the other...

bklst &# (;UserControl1.Booklist) Booklist; // cast
this.bklst.ListChanged +&# n;ew
UserControl1.BookList.ListChangedHandler(this.Booklist_Listchanged);

This seems cumbersome. Am I missing something obvious?

Also a few more questions: I have a user control called booklist and a
web form. In the user control is a list box. I know that events from that
list box must be caught in the user control. Am I right that there is no way
to let them percolate up to the enclosing page?

Thanks.

| [aspngescalate] member Click here to reveal e-mail address &# Y;OUR ID |
http://www.asplists.com/asplists/aspngescalate.asp &# J;OIN/QUIT
Reply to this message...
 
 
System.Web.UI.UserControl




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com