Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Web Controls (http://www.velocityreviews.com/forums/f63-asp-net-web-controls.html)
-   -   handling usercontrol event in container page in .net 2.0 (http://www.velocityreviews.com/forums/t780650-handling-usercontrol-event-in-container-page-in-net-2-0-a.html)

John Hopper 05-30-2008 07:53 PM

handling usercontrol event in container page in .net 2.0
 
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,

Teemu Keiski 05-30-2008 08:23 PM

Re: handling usercontrol event in container page in .net 2.0
 
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

"John Hopper" <jbhopper@newsgroups.nospam.com> wrote in message
news:FD2DA291-740B-4DA2-B452-12D1FCB4F536@microsoft.com...
> 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,




John Hopper 05-30-2008 08:58 PM

Re: handling usercontrol event in container page in .net 2.0
 
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,

"Teemu Keiski" wrote:

> 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
>
> "John Hopper" <jbhopper@newsgroups.nospam.com> wrote in message
> news:FD2DA291-740B-4DA2-B452-12D1FCB4F536@microsoft.com...
> > 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,

>
>
>


Teemu Keiski 06-01-2008 02:15 PM

Re: handling usercontrol event in container page in .net 2.0
 
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

"John Hopper" <jbhopper@newsgroups.nospam.com> wrote in message
news:D683AD86-7997-4638-93FA-72100E5CA6CB@microsoft.com...
> 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,
>
> "Teemu Keiski" wrote:
>
>> 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
>>
>> "John Hopper" <jbhopper@newsgroups.nospam.com> wrote in message
>> news:FD2DA291-740B-4DA2-B452-12D1FCB4F536@microsoft.com...
>> > 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,

>>
>>
>>




Teemu Keiski 06-01-2008 07:29 PM

Re: handling usercontrol event in container page in .net 2.0
 
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" <joteke@aspalliance.com> wrote in message
news:uZd$1H$wIHA.4916@TK2MSFTNGP03.phx.gbl...
> 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
>
> "John Hopper" <jbhopper@newsgroups.nospam.com> wrote in message
> news:D683AD86-7997-4638-93FA-72100E5CA6CB@microsoft.com...
>> 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,
>>
>> "Teemu Keiski" wrote:
>>
>>> 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
>>>
>>> "John Hopper" <jbhopper@newsgroups.nospam.com> wrote in message
>>> news:FD2DA291-740B-4DA2-B452-12D1FCB4F536@microsoft.com...
>>> > 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,
>>>
>>>
>>>

>
>




John Hopper 06-02-2008 02:04 PM

Re: handling usercontrol event in container page in .net 2.0
 
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" wrote:

> 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" <joteke@aspalliance.com> wrote in message
> news:uZd$1H$wIHA.4916@TK2MSFTNGP03.phx.gbl...
> > 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
> >
> > "John Hopper" <jbhopper@newsgroups.nospam.com> wrote in message
> > news:D683AD86-7997-4638-93FA-72100E5CA6CB@microsoft.com...
> >> 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,
> >>
> >> "Teemu Keiski" wrote:
> >>
> >>> 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
> >>>
> >>> "John Hopper" <jbhopper@newsgroups.nospam.com> wrote in message
> >>> news:FD2DA291-740B-4DA2-B452-12D1FCB4F536@microsoft.com...
> >>> > 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,
> >>>
> >>>
> >>>

> >
> >

>
>
>


John Hopper 06-02-2008 05:13 PM

Re: handling usercontrol event in container page in .net 2.0
 
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" wrote:

> 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" wrote:
>
> > 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" <joteke@aspalliance.com> wrote in message
> > news:uZd$1H$wIHA.4916@TK2MSFTNGP03.phx.gbl...
> > > 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
> > >
> > > "John Hopper" <jbhopper@newsgroups.nospam.com> wrote in message
> > > news:D683AD86-7997-4638-93FA-72100E5CA6CB@microsoft.com...
> > >> 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,
> > >>
> > >> "Teemu Keiski" wrote:
> > >>
> > >>> 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
> > >>>
> > >>> "John Hopper" <jbhopper@newsgroups.nospam.com> wrote in message
> > >>> news:FD2DA291-740B-4DA2-B452-12D1FCB4F536@microsoft.com...
> > >>> > 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,
> > >>>
> > >>>
> > >>>
> > >
> > >

> >
> >
> >



All times are GMT. The time now is 07:22 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.