I have a button in a web user control. When the user clicks the button I'd
like the web user control to raise an event to the web page.
Here's the code from the user control that raises the event to the form.
public event System.EventHandler GroupsButtonClick;
private void btnGroups_Click(object sender, System.EventArgs e)
{
this.GroupsButtonClick(this,e);
}
Here's the declaration of the user control and the method in the web form
that should run when the button in the web user control is fired:
protected SandBox._Controls.wucUser ctlUser;
private void GroupButtonClicked(Object sender, EventArgs e)
{
Response.Write("Hello!");
}
Here's the InitializeComponent line of code in the web form that connects
the two:
((_Controls.wucUser)this.FindControl("User1")).GroupsButtonClick += new
System.EventHandler(this.GroupButtonClicked);
I tried this
this.ctlUser.GroupsButtonClick += new
System.EventHandler(this.GroupButtonClicked);
but got 'Object reference not set to an instance of an object.. Seems the
protected member variable ctlUser isn't wired until sometime after the
InitializeComponent method runs.
I'm wondering if this is either taboo or if there's a simpler way to do it.
Thank you
Andy
- Add a delegate named MyEvtHandler before the class declaration:
for example:
public delegate void MyEvtHandler( object sender, System.EventArgs e );
- Add a function named FireMyEvent to the class after the class
constructor.
Make a call to another function named MyEvt in the implementation for this
class.
MyEvt function is of type MyEvtHandler. The code appears as follows:
public void FireMyEvt ( object sender, System.EventArgs e )
{
MyEvt ( sender, e );
}
public MyEvtHandler MyEvt;
- Add a class EvtRcvBase to the main program outside the definition of
Class1 that was created by the wizard.
For the newly created class, follow these steps:
-Create a constructor for the EvtRcvBase class.
-Create an instance of the EvtSource class that was created in step 3 and
name it evtSrc.
- Create a virtual function named OnMyEvt in the class definition for
EvtRcvBase, and then add a WRITELINE statement to its implementation.
- Add the subscription code for the event MyEvt provided by the EvtSource
class.
This code hooks the handler MyEvtHandler to the event MyEvt. To do this,
use the += operator to add a handler to the event in the form of a new
delegate instance that is initialized with your event handler method.
- Add another method named SomeMethod to the EvtRcvBase class and call the
FireMyEvt method on the evtSrc object.
Below I included a sample of user control that raise an event for the
hosting page.
================
UserControl2.ascx
================
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="UserControl2.ascx.cs" Inherits="CustomerDemos.UserControl2"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:Button id="Button1" Text="Button" runat="server"></asp:Button>
===================
UserControl2.ascx.cs
===================
namespace CustomerDemos
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for UserControl2.
/// </summary>
public abstract class UserControl2 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button Button1;
public delegate void ProductChangeHandler(object sender, EventArgs e);
public event ProductChangeHandler ProductChange;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
protected virtual void FireControlChange(object sender, EventArgs e)
{
if (ProductChange != null)
{
//Invokes the delegates.
ProductChange(this, e);
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
FireControlChange(this,e);
}
}
}
=================
UCEventForm.aspx
=================
<%@ Page language="c#" Codebehind="UCEventForm.aspx.cs"
AutoEventWireup="false" Inherits="CustomerDemos.UCEventForm" %>
<%@ Register TagPrefix="uc1" TagName="UserControl2" Src="UserControl2.ascx"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>UCEventForm</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="UCEventForm" method="post" runat="server">
<asp:Panel id="Panel1" style="Z-INDEX: 101; LEFT: 330px; POSITION:
absolute; TOP: 54px" runat="server">Panel</asp:Panel></form>
</body>
</HTML>
====================
UCEventForm.aspx.cs
====================
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace CustomerDemos
{
/// <summary>
/// Summary description for UCEventForm.
/// </summary>
public class UCEventForm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel Panel1;
protected UserControl2 MyUserControl;
private void Page_Load(object sender, System.EventArgs e)
{
MyUserControl = (UserControl2)Page.LoadControl("UserControl2.ascx");
Panel1.Controls.Add(MyUserControl);
MyUserControl.ProductChange += new
UserControl2.ProductChangeHandler(this.UCButtonRaised);
}
private void UCButtonRaised(object sender, EventArgs e)
{
Response.Write("Event Raised from User Control");
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Please let me know if you have any questions regarding this.
Thanks,
Bassel Tabbara
Microsoft, ASP.NET
This posting is provided "AS IS", with no warranties, and confers no rights.
--------------------
| From: "Andy" <an...@eunson.com>
| Subject: Raising Events from a web user control
| Date: Sat, 19 Apr 2003 23:49:38 -0400
| Lines: 46
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
| Message-ID: <OBLNk7uB...@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: pcp03931678pcs.walngs01.pa.comcast.net 68.86.196.215
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:10870
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols