Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional 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 September 7th, 2004, 08:15 AM
Authorized User
 
Join Date: Sep 2004
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to nashnash
Default user control

Hi

I have usercontrol and have 2 button one is save and one is delete

what i want is when i include this in user control i want to write my own event for save and delete.

lets say i included this usercontrol in "first.aspx" then i want to call some event on save and delete

and when i include the same usercotrol in "second.aspx" i want to fire soem diffrent event on save and delete.

Please suggest me the way to do it.or small example will be greatly appreciated.

Thanks in Advance....


 
Old October 25th, 2004, 11:14 AM
Authorized User
 
Join Date: Oct 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

you can not treat usercontrols events in your form, all events must be fired and treat in your control.

you can 'expose' properties.

----------------------
wagner reatto

Intel P4 2.8 Ghz | 1 Gb RAM | 120 Gb HD | NVidia | Soundblaster

Windows 2000 Pro | ADSL | Mozilla Firefox
 
Old October 29th, 2004, 01:16 PM
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 create some public events on the user control. You can then consume those events in the page code.

[u]User Control Code</u>
Public Event SaveClicked()
Public Event DeleteClicked()

Sub cmdSave_Clicked(...) Handles cmdSave.Click
    RaiseEvent SaveClicked
End Sub
Sub cmdDelete_Clicked(...) Handles cmdDelete.Click
    RaiseEvent DeleteClicked
End Sub

[u]Page Code</u>
Sub myUserControl_SaveClicked(...) Handles myUserControl.SaveClicked
    ...
End Sub
Sub myUserControl_DeleteClicked(...) Handles myUserControl.DeleteClicked
    ...
End Sub
 
Old October 30th, 2004, 08:17 AM
Authorized User
 
Join Date: Oct 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi planoie, my answer was based on "Professional ASP.NET 1.1", "Chapter 4: Writing ASP.NET Pages"

"User Control Events
The key thing to remember when dealing with user control events is that the event needs to be handled in the user control itself, and not in the page. In this way, the entire event handling for a user control is encapsulated within the control. You should not include event handlers for controls within a user control in the page that is hosting the user control (that is, the parent page) – the events will not be passed to the page, so the event will never be processed."

can you please post a code for testing?

thank you

----------------------
wagner reatto
 
Old November 8th, 2004, 12:25 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Is the code I posted above not adequate? That codes is an example of event bubbling where you handle a control event (such as a button click) within the user control that contains the control, and then raise a usercontrol event to tell consumers of the control about the event.
 
Old November 8th, 2004, 02:34 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Wagner,

I think you misread or misunderstood some of the text you quoted:
Quote:
quote:You should not include event handlers for controls within a User Control in the page that is hosting the User Control
This doesn't say that you cannot handle events from the User Control in the page; all this says is that you shouldn't include handlers for controls within a user control in the page.

So, say you have a User Control called MemberDetails that allows you to create or update information about a Member. Such a control can have a text box for the name, a text box for the e-mail address and a save button.

The UserName_Change event and the SaveButton_Click event should be handled *inside* the control (that's what the you quoted text is all about). That is because the User Control knows it has a text box called UserName and a button called SaveButton.

However, and this is what Peter is suggesting, you can fire events from within the control that bubble up to the page.

For example, you may want to fire a event like MemberDetailsUpdated. When you save the Member details in the User Control, you can invoke this event.
A page that consumes this User Control can register for this event, and when the Member Details have been saved, a label can be updated, the user can be redirected to another page, or whatever it is you want to do when the details are saved.

In addition, you can bubble up the event for a specific control directly to the parent page. In that case, the parent page can handle the SaveButton_Click event. The code Peter posted does exactly that. Notice that you aren't handling the SaveButton_Click directly; the UC intercepts the event, and then invokes its own public event called SaveClicked.

So, there is a clear distinction between handling the events for controls within the user control, and other public events you define yourself in the UC and that can be handled by the parent page.

Does this make any sense??

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Babylon by The Tea Party (Track 8 from the album: Transmission) What's This?
 
Old November 8th, 2004, 09:55 PM
Authorized User
 
Join Date: Oct 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you both!!! really a misunderstood. i am grateful for your tolerance.

----------------------
wagner reatto





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 stu9820 ASP.NET 1.0 and 1.1 Basics 20 July 20th, 2004 11:42 AM





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