dynamically adding controls with events (but events are not firing)

Discussion in 'ASP .Net' started by SevDer, Nov 13, 2007.

  1. SevDer

    SevDer Guest

    Hi,

    I have a very simple page where I have a dynamically generated control
    (panel) with child controls, but the problem is the dynamically added
    control has events and they are not fired which I expect to have a recursive
    page generations.

    Here is my aspx.
    ------------------------------------------------------------
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:panel ID="Panel1" runat="server" Height="54px" Width="140px">
    <asp:Label ID="lTitle" runat="server" Text="My Title"></asp:Label><br />
    <asp:Button ID="bPopulate" runat="server" Text="Populate Me"
    OnClick="PopulateChildren" CommandArgument="1" /></asp:panel>
    </div>
    </form>
    </body>

    And here is the code behind to generate the dynamic content.
    ------------------------------------------------------------
    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    ArrayList dynamicContent;
    if (Session["MyItems"] != null)
    {
    dynamicContent = Session["MyItems"] as ArrayList;
    foreach (Panel toAddPanel in dynamicContent)
    {
    Page.Form.Controls.Add(toAddPanel);
    }
    }

    }

    protected void PopulateChildren(object sender, EventArgs e)
    {
    Button parentButton = (Button)sender;

    Panel childPanel = new Panel();
    childPanel.EnableViewState = true;

    Label childLabel = new Label();
    childLabel.Text = "Child of " +
    ((Label)((Panel)(parentButton).Parent).Controls[1]).Text;
    childLabel.EnableViewState = true;

    Literal childLiteral = new Literal();
    childLiteral.Text = "<br>";
    childLiteral.EnableViewState = true;

    Button childButton = new Button();
    childButton.Text = "Child of " + ((Button)sender).Text;
    childButton.Click += new EventHandler(PopulateChildren);
    childButton.EnableViewState = true;

    childPanel.ID = "p" + (int.Parse(parentButton.CommandArgument) +
    1).ToString();
    childLabel.ID = "cl" + (int.Parse(parentButton.CommandArgument) +
    1).ToString();
    childLiteral.ID = "clt" + (int.Parse(parentButton.CommandArgument) +
    1).ToString();
    childButton.ID = "cb" + (int.Parse(parentButton.CommandArgument) +
    1).ToString();

    childPanel.Controls.Add(childLabel);
    childPanel.Controls.Add(childLiteral);
    childPanel.Controls.Add(childButton);

    ArrayList dynamicContent;
    if (Session["MyItems"] != null)
    dynamicContent = Session["MyItems"] as ArrayList;
    else
    dynamicContent = new ArrayList();
    dynamicContent.Add(childPanel);
    Session["MyItems"] = dynamicContent;


    Page.Form.Controls.Add(childPanel);


    }
    }

    Here I am not insisting on the part that I have in Page_Load but some way I
    need to persist the controls in the page and have the events from those
    fired.

    Can anyone guide me please?

    SevDer
    http://www.sevder.com
     
    SevDer, Nov 13, 2007
    #1
    1. Advertising

  2. SevDer

    bruce barker Guest

    you can not store the dynamic controls in session. they belong to the
    previous page, and becuase they hold a delegat, those pages object are
    held, and events if fired, run in the old pages. this also leads to
    great memory leaks.

    also as every page request is a new class instance, you need to attach
    event handlers.

    you should store info in session that allows you to recreate the dynamic
    content. you shouls also recreate at oninit, not pageload.

    -- bruce (sqlwork.com)

    SevDer wrote:
    > Hi,
    >
    > I have a very simple page where I have a dynamically generated control
    > (panel) with child controls, but the problem is the dynamically added
    > control has events and they are not fired which I expect to have a recursive
    > page generations.
    >
    > Here is my aspx.
    > ------------------------------------------------------------
    > <body>
    > <form id="form1" runat="server">
    > <div>
    > <asp:panel ID="Panel1" runat="server" Height="54px" Width="140px">
    > <asp:Label ID="lTitle" runat="server" Text="My Title"></asp:Label><br />
    > <asp:Button ID="bPopulate" runat="server" Text="Populate Me"
    > OnClick="PopulateChildren" CommandArgument="1" /></asp:panel>
    > </div>
    > </form>
    > </body>
    >
    > And here is the code behind to generate the dynamic content.
    > ------------------------------------------------------------
    > public partial class _Default : System.Web.UI.Page
    > {
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > ArrayList dynamicContent;
    > if (Session["MyItems"] != null)
    > {
    > dynamicContent = Session["MyItems"] as ArrayList;
    > foreach (Panel toAddPanel in dynamicContent)
    > {
    > Page.Form.Controls.Add(toAddPanel);
    > }
    > }
    >
    > }
    >
    > protected void PopulateChildren(object sender, EventArgs e)
    > {
    > Button parentButton = (Button)sender;
    >
    > Panel childPanel = new Panel();
    > childPanel.EnableViewState = true;
    >
    > Label childLabel = new Label();
    > childLabel.Text = "Child of " +
    > ((Label)((Panel)(parentButton).Parent).Controls[1]).Text;
    > childLabel.EnableViewState = true;
    >
    > Literal childLiteral = new Literal();
    > childLiteral.Text = "<br>";
    > childLiteral.EnableViewState = true;
    >
    > Button childButton = new Button();
    > childButton.Text = "Child of " + ((Button)sender).Text;
    > childButton.Click += new EventHandler(PopulateChildren);
    > childButton.EnableViewState = true;
    >
    > childPanel.ID = "p" + (int.Parse(parentButton.CommandArgument) +
    > 1).ToString();
    > childLabel.ID = "cl" + (int.Parse(parentButton.CommandArgument) +
    > 1).ToString();
    > childLiteral.ID = "clt" + (int.Parse(parentButton.CommandArgument) +
    > 1).ToString();
    > childButton.ID = "cb" + (int.Parse(parentButton.CommandArgument) +
    > 1).ToString();
    >
    > childPanel.Controls.Add(childLabel);
    > childPanel.Controls.Add(childLiteral);
    > childPanel.Controls.Add(childButton);
    >
    > ArrayList dynamicContent;
    > if (Session["MyItems"] != null)
    > dynamicContent = Session["MyItems"] as ArrayList;
    > else
    > dynamicContent = new ArrayList();
    > dynamicContent.Add(childPanel);
    > Session["MyItems"] = dynamicContent;
    >
    >
    > Page.Form.Controls.Add(childPanel);
    >
    >
    > }
    > }
    >
    > Here I am not insisting on the part that I have in Page_Load but some way I
    > need to persist the controls in the page and have the events from those
    > fired.
    >
    > Can anyone guide me please?
    >
    > SevDer
    > http://www.sevder.com
    >
    >
     
    bruce barker, Nov 13, 2007
    #2
    1. Advertising

  3. Hi SevDer,

    In addition to Bruce's reply, you may find following KBs useful:

    #HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET
    http://support.microsoft.com/kb/317794

    #HOW TO: Dynamically Create Controls in ASP.NET with Visual Basic .NET
    http://support.microsoft.com/kb/317515


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Nov 13, 2007
    #3
    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. John Hughes
    Replies:
    1
    Views:
    3,073
    Karl Seguin
    Aug 11, 2005
  2. aliciahogue
    Replies:
    0
    Views:
    612
    aliciahogue
    Oct 20, 2009
  3. nitin

    Event not firing. Adding controls dynamically to UserControl

    nitin, Jul 16, 2003, in forum: ASP .Net Building Controls
    Replies:
    5
    Views:
    174
    Patrick Sullivan
    Jul 23, 2003
  4. David Boike

    Child controls in Composite Controls not firing events

    David Boike, Aug 7, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    316
    David Boike
    Aug 7, 2003
  5. Robert Sheppard

    Dynamically Created Controls Not Firing Events

    Robert Sheppard, Oct 28, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    256
    Prodip Saha
    Oct 28, 2005
Loading...

Share This Page