.NETGURU
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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification