Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7563693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:43:42+00:00 2026-05-30T13:43:42+00:00

I am trying to create a UI that creates rows that can be dynamically

  • 0

I am trying to create a UI that creates rows that can be dynamically populated.

I am at the point where I can add a Panel to my list that contains a DropDownList and has an associated Remove Button.

The Remove Button has an OnClick event bound that allows me to remove that specific panel.

I am having an issue when trying to bind a SelectedIndexChanged EventHandler to my DropDownList it does not.

I suspect this had something to do with how I am recreating my controls after every Postback.

I am not asking for or expecting a straightforward “Add this or Change this in the code.” I really want to have a understand where I am going wrong, sorry for asking so much! :s

protected void Page_Load(object sender, EventArgs e)
{
    //Showing this for the the poster that asked.
    if (!IsPostBack)
    {
        GetClustersFromDB(user);
        BindGrid();
        BindState();            
    }
    if (Session["persistControls"] != null)
    {
        persistControls = (List<Panel>)Session["persistControls"];
        int count = 0;

        foreach (Panel dynamicControl in persistControls)
        {
            DropDownList list = new DropDownList();
            list.ID = "list" + count;
            list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
            list.AutoPostBack = true;
            list.Items.Add(new ListItem("", "0"));
            list.Items.Add(new ListItem("Title", "1"));

            dynamicControl.ID = "panel" + count;

            Button btnRemove = new Button();
            btnRemove.Click += new EventHandler(btnDelete_Click);
            btnRemove.Text = "Remove";
            btnRemove.CommandArgument = count.ToString();             

            myPlaceholder.Controls.Add(dynamicControl);
            myPlaceholder.Controls.Add(btnRemove);
            count++;
        }
    }
}

protected void btnAdd_Click(object sender, EventArgs e)
{
    try
    {
        DropDownList list = new DropDownList();
        list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
        list.AutoPostBack = true;
        list.Items.Add(new ListItem("", "0"));
        list.Items.Add(new ListItem("Title", "1"));

        Panel panelContainer = new Panel();
        panelContainer.ID = "panel" + persistControls.Count;

        panelContainer.Controls.Add(list);

        Button btnRemove = new Button();
        btnRemove.Click += new EventHandler(btnDelete_Click);
        btnRemove.Text = "Remove";
        btnRemove.CommandArgument = persistControls.Count.ToString();
        myPlaceholder.Controls.Add(panelContainer); // Pushes the Panel to the page.
        persistControls.Add(panelContainer);// Adds our Panel to the Control list

        myPlaceholder.Controls.Add(btnRemove); // Pushes our Button to the page.
        Session["persistControls"] = persistControls; // put it in the session
    }
    catch
    {
        throw;
    }
}

protected void btnDelete_Click(object sender, EventArgs e)
{
    try
    {
        int deleteThisOne = int.Parse(((Button)sender).CommandArgument);
        persistControls.Remove(persistControls[deleteThisOne]);
        Session["persistControls"] = persistControls;          
        Response.Redirect(Request.Url.ToString());
    }
    catch
    {
        throw;
    }
}

protected void list_SelectedIndexChanged(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

//aspx File Snippets
<asp:Button ID="btnAdd" runat="server" Text="Add Control" onclick="btnAdd_Click" />
<asp:Button ID="btnClear" runat="server" Text="Reset" onclick="btnClear_Click"/>
<asp:PlaceHolder ID="myPlaceholder" runat="server"></asp:PlaceHolder>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:43:43+00:00Added an answer on May 30, 2026 at 1:43 pm

    Maintaining controls like this is a real pain. You could try an alternative approach:

    1. Create a (small) class that encapsulates the data you want to display in each Panel.
    2. Maintain a List of these objects in Session.
    3. Use an <asp:Repeater> to display the items by data-binding it to your list.
    4. In the repeater’s ItemTemplate, you can write out your <asp:Panel> that can contain a <asp:DropDownList>. Use the ItemCommand property of the drop-down list to bind it to a server-side event handler. Similarly you can put an <asp:Button> in the template and bind the ItemCommand property of this to another server-side event handler.
    5. When you click Add, you’ll postback to the server, add to your list, save it in session, re-bind the repeater.
    6. When you click a Remove, you’ll postback to the server, remove the item from your list, save it in session, re-bind the repeater.

    This way lets ASP.Net handle all the control creation for you. However, if you really want to do all that yourself, then this Infinities Loop blog post is required reading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a panel that can dynamically insert components based on
I'm trying to create a feature that both creates a list template and an
I trying to create grid that contains rows with the following structure: item name,
I'm trying to create a jTable that, once a button is clicked, adds rows
I'm trying to create a form that has an expandable widget. The problem is
I'm trying to create a Django app where the user can make a list
I am trying to create an array or a list where I can put
I am trying to add rows to a TableLayout that I define in an
I am trying to create a UITableView that only displays the number of rows
I'm trying to create models that represent a swiss tournament , with multiple rounds.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.