Eventhandling from dynamically created controls

Discussion in 'ASP .Net Building Controls' started by mrbentzen@gmail.com, Oct 13, 2005.

  1. Guest

    I have some questions reguarding event handling of dynamically added
    controls.

    An example scenario (se code below):
    I have one button declared in the aspx file. When the button is
    clicked, the OnCommand event is fired.
    The evanthandler catching this event, creates a new button dynamically
    and ads it to a placeholders control collection.
    This new button also has an event handler which ads another button etc.
    But as this button does not exsist when the postback is processed, the
    event from the second button cannot be caught.

    Is there any way to be able to catch this event?

    This is ofcouse a very simplified version of the real problem, the
    solution consists of several, more complex databound control.
    If it's possible, it is desired that the control is not databound more
    than once, as thisfor som controls costs quite a bit of CPU time.

    I know that you would normally just recreate the controls in Page.Load
    o.a., but this is a quite complex solution with controls in controls
    (etc.) and the page does therefore not know all the dynamically created
    controls directly.
    I have seen one solution where the controls are added to Session, but
    this is an undesired solution in our case.

    If, by any chance there are any Microsoft designers listening in, I
    would like to ask what the official correct way to handle events from
    dynamically created controls is. I have looked at the Paterns and
    Practices pages, but not found any clear answer to the problem.

    /Bentzen

    *-----EventSample.aspx-----*
    <%@ Page Language="C#" AutoEventWireup="true"
    CodeFile="EventSample.aspx.cs" Inherits="test_EventSample" %>
    <!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 id="Head1" runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <a href="<%=Page.Request.Url %>">Reload</a><br /><br />
    <asp:Button ID="Btn1" runat="server" Text="p1"
    OnCommand="Ctrl_OnCommand"/>
    <asp:placeHolder ID="PH1" runat="server"/>
    </div>
    </form>
    </body>
    </html>

    *-----EventSample.aspx.cs-----*
    public partial class test_EventSample : System.Web.UI.Page{
    protected void Ctrl_OnCommand(object sender, CommandEventArgs e){
    AddNewButton("New - " + ((Control)sender).ClientID, PH1);
    }

    private void AddNewButton(string name, PlaceHolder PH){
    Button NewBtn = new Button();
    NewBtn.Text = name;
    NewBtn.Command += new CommandEventHandler(Ctrl_OnCommand);
    PH.Controls.Add(NewBtn);
    }
    }
    , Oct 13, 2005
    #1
    1. Advertising

  2. In your example below you could have preserved the event had you also
    recreated the new button upon loading the page; which you could have done had
    you saved any indicator in the Session object that identifies the added
    control.

    In this demo I create both server and HTML controls during handling of an
    event but I recreated them upon Page_Load also:
    http://www.societopia.net/Samples/DynamicallyCreatedControls.aspx

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "" wrote:

    > I have some questions reguarding event handling of dynamically added
    > controls.
    >
    > An example scenario (se code below):
    > I have one button declared in the aspx file. When the button is
    > clicked, the OnCommand event is fired.
    > The evanthandler catching this event, creates a new button dynamically
    > and ads it to a placeholders control collection.
    > This new button also has an event handler which ads another button etc.
    > But as this button does not exsist when the postback is processed, the
    > event from the second button cannot be caught.
    >
    > Is there any way to be able to catch this event?
    >
    > This is ofcouse a very simplified version of the real problem, the
    > solution consists of several, more complex databound control.
    > If it's possible, it is desired that the control is not databound more
    > than once, as thisfor som controls costs quite a bit of CPU time.
    >
    > I know that you would normally just recreate the controls in Page.Load
    > o.a., but this is a quite complex solution with controls in controls
    > (etc.) and the page does therefore not know all the dynamically created
    > controls directly.
    > I have seen one solution where the controls are added to Session, but
    > this is an undesired solution in our case.
    >
    > If, by any chance there are any Microsoft designers listening in, I
    > would like to ask what the official correct way to handle events from
    > dynamically created controls is. I have looked at the Paterns and
    > Practices pages, but not found any clear answer to the problem.
    >
    > /Bentzen
    >
    > *-----EventSample.aspx-----*
    > <%@ Page Language="C#" AutoEventWireup="true"
    > CodeFile="EventSample.aspx.cs" Inherits="test_EventSample" %>
    > <!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 id="Head1" runat="server">
    > <title>Untitled Page</title>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    > <div>
    > <a href="<%=Page.Request.Url %>">Reload</a><br /><br />
    > <asp:Button ID="Btn1" runat="server" Text="p1"
    > OnCommand="Ctrl_OnCommand"/>
    > <asp:placeHolder ID="PH1" runat="server"/>
    > </div>
    > </form>
    > </body>
    > </html>
    >
    > *-----EventSample.aspx.cs-----*
    > public partial class test_EventSample : System.Web.UI.Page{
    > protected void Ctrl_OnCommand(object sender, CommandEventArgs e){
    > AddNewButton("New - " + ((Control)sender).ClientID, PH1);
    > }
    >
    > private void AddNewButton(string name, PlaceHolder PH){
    > Button NewBtn = new Button();
    > NewBtn.Text = name;
    > NewBtn.Command += new CommandEventHandler(Ctrl_OnCommand);
    > PH.Controls.Add(NewBtn);
    > }
    > }
    >
    >
    Phillip Williams, Oct 14, 2005
    #2
    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. Andrea
    Replies:
    0
    Views:
    364
    Andrea
    Dec 18, 2003
  2. Claes Rådström
    Replies:
    3
    Views:
    329
    Claes Rådström
    Feb 5, 2004
  3. Herfried K. Wagner [MVP]
    Replies:
    12
    Views:
    551
    =?Utf-8?B?YmFsbWVyY2g=?=
    Dec 1, 2005
  4. cm
    Replies:
    2
    Views:
    1,992
  5. msimmons
    Replies:
    0
    Views:
    452
    msimmons
    Jul 16, 2009
Loading...

Share This Page