Problem when working with user control's event

Posted by Chandan.sahab under Regular Expressions on 1/9/2010 | Views : 2956 | Status : [Member] | Replies : 9
Hi,
In my current Prj,i have several user controls with their special functionalities, and in a web form i'm dynamically calling any one of them on certain conditions.I am facing problem when i firing an event of the called user control on the web page, like in the called user control there are several dropdown and also their events like "onselectedindechanged". but these events are not working on the parent page.
Please help me to resolve this or suggest better idea for that.

Chandan.




Responses

Posted by: Abhi2434 on: 1/10/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Why dont you expose some event from the user Control?
Say you expose events like DropDown1SelectedIndexChanged, DropDown2SelectedIndexChanged etc and raise them when the actual event occurs.

To expose an event you need to declare a delegate. Like this :

public delegate void DropDownSelectionChanged(object sender, SelectionChangeEventArgs e);


Define an Event :

public event DropDownSelectionChanged DropDown1SelectedIndexChanged;

public event DropDownSelectionChanged DropDown2SelectedIndexChanged;


You also need to define some virtual methods to check whether the event raised actually added to any handler. To do this Use:

public virtual void OnDropDown1SelectionChanged(object sender, SelectionChangeEventArgs e)

{
if(this.DropDown1SelectedIndexChanged != null)
this.DropDown1SelectedIndexChanged(sender, e);
}
public virtual void OnDropDown2SelectionChanged(object sender, SelectionChangeEventArgs e)
{
if(this.DropDown2SelectedIndexChanged != null)
this.DropDown2SelectedIndexChanged(sender, e);
}



Finally in the actual SelectedIndexChanged event :

protected void dropdown1_selectedIndexChanged(object sender, EventArgs e)
{
SelectionChangeEventArgs eventargs = new SelectionChangedEventArgs(dropdown1.SelectedItem);
this.OnDropDown1SelectionChanged(this, eventArgs);
}

For this you also need to define a custom class SelectionChangeEventArgs.

public class SelectionChangeEventArgs

{
public SelectionChangeEventArgs(ListItem li)
{
this.SeletedItem = li;
}
public ListItem SelectedItem
{ get; set;}
}


Now fianlly in the calling environment, you need to add one eventhandler for DropDown1SelectionChanged and do whatever you want.
Also modify the class SelectionChangeEventArgs to define the items you need to pass into the calling environment.

www.abhisheksur.com

Chandan.sahab, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: RtpHarry on: 1/10/2010 [Member] [MVP] Bronze

Up
0
Down
Abhi2434 has covered this pretty well in their reply. I just wanted to add a couple of links to two tutorials which I have used as refreshers when I needed to do this in my projects as they have come in handy several times.

This first one was my original and best:
http://codebetter.com/blogs/brendan.tompkins/archive/2004/10/06/Easily-Raise-Events-From-ASP.NET-ASCX-User-Controls.aspx

And this one is part of a series which shows you how to take advantage of a couple of features of UserControls:
http://visualstudiomagazine.com/articles/2009/03/25/adding-events-to-user-controls.aspx

Chandan.sahab, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chandan.sahab on: 1/11/2010 [Member] Starter

Up
0
Down
Thnks for the response,
I'm going through the articles suggested by RtpHarry. Thanks to Abhi2434 & RtpHarry once again.

Chandan.

Chandan.sahab, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 1/11/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Great.

Thanks.

www.abhisheksur.com

Chandan.sahab, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Puneet20884 on: 1/11/2010 [Member] Bronze

Up
0
Down
Hi Chandan,
You can proceed like follows.

Create a delegate in your usercontrol (public delegate).

Create an Event of this delegate (will be public)

On the event in which you want to fire conditionally from the web page, use your event in that Event.

Define this event in the page where you added your control.

When the event in which you have used you Event, will fire, the code code that you deined in it will fire.

It will definitly resolve your problem as was one of my major initial problem.



Best Regards,
Puneet Sharma - Infosys
Pune, India

Chandan.sahab, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Virendradugar on: 1/11/2010 [Member] [MVP] Silver

Up
0
Down
Hi Chandan,

Check this article.

http://www.dotnetfunda.com/articles/article569-capture-buttons-click-event-of-user-control-in-the-page-.aspx

Hope this helps!!!!

Chandan.sahab, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nishithraj on: 1/12/2010 [Member] Bronze

Up
0
Down
Chandan,

Whether any of the above solutions have worked out. If yes, can you kindly inform which one helped you & how did you resolve this issue to help others with the same kind of issues.

Thank you

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Chandan.sahab, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Puneet20884 on: 1/12/2010 [Member] Bronze

Up
0
Down
Of Cos Chandan,

http://www.dotnetfunda.com/articles/article767-simple-custom-event--delegates-demonstration-.aspx

check above link, you may get the track to resolve your problem

Best Regards,
Puneet Sharma - Infosys
Pune, India

Chandan.sahab, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 1/13/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Also you can take a look into this article I have produced :
http://www.codeproject.com/Articles/51671/How-To-Expose-events-from-UserControl.aspx

I hope this will help you. Also try out the Sample example I have provided.

Cheers.

www.abhisheksur.com

Chandan.sahab, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response