Here's a sample for you:
WebForm1.aspx
=============
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="WebApplication2.WebForm1" %>
<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1"
tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<uc1:WebUserControl1 ID="ucSample" runat="server"
OnClick="ucSample_Click" />
</form>
</body>
</html>
WebForm1.aspx.cs
================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ucSample_Click(object sender, EventArgs e)
{
Response.Write("User control's Click event was raised! " +
DateTime.Now.ToString());
}
}
}
WebUserControl1.ascx
====================
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="WebUserControl1.ascx.cs"
Inherits="WebApplication2.WebUserControl1" %>
<asp:Button ID="Button1" runat="server" />
WebUserControl1.ascx.cs
=======================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
namespace WebApplication2
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
#region "USer control's Click Event"
protected static readonly object EventClick = new object();
public event EventHandler Click
{
add
{
Events.AddHandler(EventClick, value);
}
remove
{
Events.AddHandler(EventClick, value);
}
}
protected virtual void OnClick(EventArgs e)
{
EventHandler eh = Events[EventClick] as EventHandler;
if (eh != null)
eh(this, e);
}
#endregion
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Button1.Click += new EventHandler(Button1_Click);
}
void Button1_Click(object sender, EventArgs e)
{
OnClick(EventArgs.Empty);
}
}
}
--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
Teemu Keiski said:
There shouldn't be anything too special in doing this. Can you post a
small sample of the usercontrol you have?
--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
Thank you for your help. This was the approach I started, but for some
reason
I can't seem to handle the click event in the usercontrol to raise my
custom
event. Does it matter that the usercontrol is in an aspx page with a
master
page? There is nothing listed in the usercontrol's codebehind except page
events. When I try to assign the usercontrol button's click event
declaratively to a sub in the codebehind I get "mySub is not a member of
asp.controls_myUsercontrol_ascx runtime error.
Thank you,
:
Hi,
define an event in the user control. Then handle Button's click in the
uc as
well, and in the handler method raise the event you defined in the
previous
step. The page can then attach event handler for the user control's
event.
--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
Hello,
I want to change a property in a page when a button in a usercontrol
in
that
same page is clicked. Can someone tell me what I need in the user
control
and
in the container aspx page?
Thank you,