Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need to fire up an event through a user control that is on an aspx page

Posted on 2009-05-18
1
Medium Priority
?
247 Views
Last Modified: 2012-05-07
I have a user control on a aspx page and when the user selects an item I want an
event thrown SelectedIndexChanged += new
EventHandler(_ddlFields_SelectedIndexChanged);

in the behind of the aspx.cs page.

Features:&nbsp;<KORE:uxLookupTicketFeatures runat="server" ID="uxLookupTicketFeatures" KeyField="Features"
                        ValueField="Features" CssClass="FormFieldLayoutFormElement" />

<asp:Button ID="btnSubmitFeatures" runat="server"
        onclick="btnSubmitFeatures_Click" Text="Submit" />
0
Comment
Question by:mathieu_cupryk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 20

Accepted Solution

by:
ddayx10 earned 2000 total points
ID: 24418381
This article is simple, short, and describes how to do exactly what you are trying to accomplish:

http://weblogs.asp.net/jdanforth/archive/2005/04/06/397319.aspx

I'd feel like I was cheating if I copied and pasted it over here without a reference to the link...

This sample lets the user control raise an event, which the parent page catches. The event contains information about what text the label should be set to.

First you define the event argument class so you have a way to send data to the listener. This sample is really simple, and contains just a single text property, which will contain a string:
 


public class MyEventArgs : EventArgs
{
           private readonly string text;
 

           public MyEventArgs(string text)
           {
                      this.text = text;
           }

 

           public string Text
           {
                      get { return text; }
           }
}
 

Then you go into your user control class and set up the event handler and code to trigger/raise the event with the correct argument values. One could argue if it's really necessary to have the OnEvent() method, but I prefer that, it feels cleaner. I also define the delegate for the event handler in the same class file, you may want to have this in some other place:
 

public delegate void MyEventHandler(object sender, MyEventArgs e);
 

public class WebUserControl1 : System.Web.UI.UserControl
{
           protected System.Web.UI.WebControls.Button Button1;
           public event MyEventHandler MyEvent;
 

           private void Button1_Click(object sender, System.EventArgs e)
           {
                      OnEvent(new MyEventArgs("Time is " + DateTime.Now));
           }
 

           protected virtual void OnEvent(MyEventArgs e)
           {
                      if (MyEvent != null)
                                 MyEvent(this,e);
           }
}
 

Now, the rest of the code goes into the web form class, where you have to add the event listener after youve loaded the control. The Page_Load() event loads the control and sets up the event handler for MyEvent, called ctrl_MyEvent(). When the event fires, you just extract the text property from the event arguments and set the label text to that value.

 

Its a bit harder if you need to load different kinds of controls dynamically, but in that case I would define the events in a superclass that all user controls inherit from:
 

public class WebForm1 : System.Web.UI.Page
{
           protected System.Web.UI.WebControls.Panel Panel1;
           protected System.Web.UI.WebControls.Label Label1;
           protected WebUserControl1 ctrl = null;
 

           private void Page_Load(object sender, System.EventArgs e)
           {
                      ctrl = (WebUserControl1)LoadControl("WebUserControl1.ascx");
                      Panel1.Controls.Add(ctrl);
                      ctrl.MyEvent +=new MyEventHandler(ctrl_MyEvent);
           }
 

           private void ctrl_MyEvent(object sender, MyEventArgs e)
           {
                      Label1.Text = e.Text;
           }
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question