0

I'm doing some test with dynamic controls. Here the code:

ASPX page

<!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>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Value="0">Nothing</asp:ListItem>
            <asp:ListItem Value="1">Two buttons</asp:ListItem>
        </asp:DropDownList>
        <asp:Panel ID="Panel1" runat="server">
        </asp:Panel>
    </div>
    </form>
</body>
</html>

Code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["CreateDynamicButton"] != null)
        {
            CreateControls();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateControls();
    }

    void Button_Click(object sender, EventArgs e)
    {
        Button b = (Button)sender;

        Response.Write("You clicked the button with ID " + b.ID);
    }

    private void CreateControls()
    {
        if (DropDownList1.SelectedValue.Equals("1"))
        {
            Panel1.Controls.Clear();

            Button b1 = new Button();
            b1.ID = "b1";
            b1.Text = "Button 1";

            Button b2 = new Button();
            b2.ID = "b2";
            b2.Text = "Button 2";

            b1.Click += new EventHandler(Button_Click);
            b2.Click += new EventHandler(Button_Click);

            Panel1.Controls.Add(b1);
            Panel1.Controls.Add(b2);

            ViewState["CreateDynamicButton"] = true;
        }
    }
}

This code works but as you can see I remove all controls in the Panel1.Controls before add the buttons becouse when I choose to create them for the second time I get an exeption for duplicated controls ID.

I think that for two buttons the operation is very fast but with a larger number of controls the elaboration time will be longer. Can you suggest me a better way to re-generate controls after PostBack without this workaround?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
chenny
  • 769
  • 2
  • 17
  • 44

2 Answers2

0

For starters, if you're trying to remove the controls (in this case buttons) for good, are you best off using the Dispose method? clearing the panel of the control won't dispose of it, which would explain the already in use ID's. You can do a simple loop to do this, regardless of how many controls are in the panel;

foreach(Control control in Container)
{
  control.Dispose();
}

also, to create buttons i see you're just naming them btn1, btn2 and so on. You could do this in a loop too;

for(int i = 0; i >= yourInt; i++)
{
  Button b = new Button();
  b.ID = "b" + i;
  b.Text = "Button " + i;
}

}

Kestami
  • 2,045
  • 3
  • 32
  • 47
0

Create a class variable private bool ControlsCreated = false; In your CreateControls method then check

if (!ControlsCreated) {
    //your code to create controls
}

This makes sure the controls are created only once. If at any point later you need to recreate the controls (dropdownlist value changed), just clear the container and set ControlsCreated to false.

Destrictor
  • 752
  • 1
  • 4
  • 15