handling usercontrol event in container page in .net 2.0

Discussion in 'ASP .Net Web Controls' started by John Hopper, May 30, 2008.

  1. John Hopper

    John Hopper Guest

    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, May 30, 2008
    #1
    1. Advertising

  2. John Hopper

    Teemu Keiski Guest

    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" <> wrote in message
    news:...
    > 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, May 30, 2008
    #2
    1. Advertising

  3. John Hopper

    John Hopper Guest

    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" <> wrote in message
    > news:...
    > > 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, May 30, 2008
    #3
  4. John Hopper

    Teemu Keiski Guest

    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" <> wrote in message
    news:D...
    > 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" <> wrote in message
    >> news:...
    >> > 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, Jun 1, 2008
    #4
  5. John Hopper

    Teemu Keiski Guest

    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" <> wrote in message
    news:uZd$1H$...
    > 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" <> wrote in message
    > news:D...
    >> 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" <> wrote in message
    >>> news:...
    >>> > 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, Jun 1, 2008
    #5
  6. John Hopper

    John Hopper Guest

    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" <> wrote in message
    > news:uZd$1H$...
    > > 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" <> wrote in message
    > > news:D...
    > >> 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" <> wrote in message
    > >>> news:...
    > >>> > 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, Jun 2, 2008
    #6
  7. John Hopper

    John Hopper Guest

    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" <> wrote in message
    > > news:uZd$1H$...
    > > > 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" <> wrote in message
    > > > news:D...
    > > >> 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" <> wrote in message
    > > >>> news:...
    > > >>> > 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, Jun 2, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Ramsay

    Dynamic Datagrid UserControl Event Handling

    Colin Ramsay, Apr 17, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    797
    Colin Ramsay
    Apr 17, 2004
  2. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    213
  3. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    211
    vatech1993
    Dec 11, 2004
  4. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    183
  5. mpaine

    Declarative event handling from usercontrol

    mpaine, Sep 30, 2009, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    819
    mpaine
    Sep 30, 2009
Loading...

Share This Page