handling usercontrol event in container page in .net 2.0

J

John Hopper

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,
 
T

Teemu Keiski

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.
 
J

John Hopper

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,
 
T

Teemu Keiski

There shouldn't be anything too special in doing this. Can you post a small
sample of the usercontrol you have?
 
T

Teemu Keiski

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
 
J

John Hopper

Thank you very much for this sample. It is very helpful, however I'm having a
little trouble translating it to VB. Could you provide a VB example of the
usercontrol's code?

Thank you,

John Hopper

Teemu Keiski said:
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
 
J

John Hopper

I seem to have it now in VB but I hope I haven't overlooked anything. I
declare an event handler in the init event of the hosting page ...
-------------
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Init
AddHandler L2LT.EditClick, AddressOf L2LT_EditClicked
End Sub
----------

And define and raise an event in the usercontrol when a button is clicked ...
----------
Public Event EditClick()

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
RaiseEvent EditClick()
End Sub

-----------
and that seems to work.

Please let me know if I'm missing something.

Thanks for your help!



John Hopper said:
Thank you very much for this sample. It is very helpful, however I'm having a
little trouble translating it to VB. Could you provide a VB example of the
usercontrol's code?

Thank you,

John Hopper

Teemu Keiski said:
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top