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

  • Home
  • SEARCH
  • 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 8883105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:44:07+00:00 2026-06-14T20:44:07+00:00

The .aspx file has it’s usual html code and a Panel1 control in it.

  • 0

The .aspx file has it’s usual html code and a “Panel1” control in it.
First of all i’m creating a dropdownlist called “ddl” and add to it some ListItems, all this from the code behind. Next i want to create a certain number of other DropDownLists and copy to them all the ListItems I added to the “ddl”, after that i need them to be added to a “Panel1” control when the page is running. The most important part is that I want all the dynamically created dropdownlists to have a selected value when running the page. You can see the code below:

protected void Page_Load(object sender, EventArgs e)
{
    DropDownList ddl = new DropDownList();
    ddl.Items.Add(new ListItem("One", "1"));
    ddl.Items.Add(new ListItem("Two", "2"));
    ddl.Items.Add(new ListItem("Three", "3"));
    ddl.Items.Add(new ListItem("Four", "4"));
    ddl.Items.Add(new ListItem("Five", "5"));
    ddl.Items.Add(new ListItem("Six", "6"));
    ddl.Items.Add(new ListItem("Seven", "7"));

    int j = 2;
    for (int h = 0; h < 3; h++)
    {
        DropDownList ddlDynamic = new DropDownList();

        //Add the items from ddl to the new dropdownlsit
        for (int i = 0; i < ddl.Items.Count; i++)
        {
            ddlDynamic.Items.Add(ddl.Items[i]);
        }
        //the selected item in the first dropdownlist 
        //must be "Two" but it will be "Four". WHY???
        ddlDynamic.SelectedValue = j.ToString();
        ddlDynamic.ID = h.ToString();
        Panel1.Controls.Add(ddlDynamic);
        Panel1.Controls.Add(new LiteralControl("<br />"));
        j++;
    }
}

The problem here is that the selected value for all three dropdownlists will be the same and it will be “Four”, when logically the first must be “Two”, second “Three” and the third must has “Four” as selected value. First question is: What am I doing wrong?

Second question.
When using ddlDynamic.Items.FindByValue(j.ToString()).Selected = true; instead of ddlDynamic.SelectedValue = j.ToString(); I am getting a “Cannot have multiple items selected in a DropDownList.” Why is that?

Thank You.

  • 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-06-14T20:44:08+00:00Added an answer on June 14, 2026 at 8:44 pm

    I found the answer. The important thing here is to remember that ddlDynamic.Items.Add(ddl.Items[i]); line, adds the items by reference from the “ddl” dropdownlist, and does not copy the ListItems into each DropDownLists created dynamically with the “for” loop. All the dropdownlists created dynamically are using THE SAME ListItems initialized before and added to the “ddl”. It’s important to know that:

    ddlDynamic.DataSource = ddl.Items;
            ddlDynamic.DataBind();
    

    will do the same thing: will use the ListItems by reference from the “ddl”. So the solution i found is to create ListItems for each dynamically created DropDownList, and populate them from a source, in that case it will be a DataTable:

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("text", typeof(string));
        table.Columns.Add("value", typeof(string));
    
        table.Rows.Add("One", "1");
        table.Rows.Add("Two", "2");
        table.Rows.Add("Three", "3");
        table.Rows.Add("Four", "4");
        table.Rows.Add("Five", "5");
        table.Rows.Add("Six", "6");
        table.Rows.Add("Seven", "7");
    
        int j = 1;
        for (int h = 0; h < 3; h++)
        {
            DropDownList ddlDynamic = new DropDownList();
            foreach (DataRow rw in table.Rows)
            {
                ddlDynamic.Items.Add(new ListItem((string)rw["text"], (string)rw["value"]));
            }
    
            ddlDynamic.SelectedValue = j.ToString();
            ddlDynamic.ID = h.ToString();
            Panel1.Controls.Add(ddlDynamic);
            Panel1.Controls.Add(new LiteralControl("<br />"));
            j++;
        }
    }
    

    If someone has a better idea, please let me know.

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

Sidebar

Related Questions

I have an updateprogress in my aspx file, which has a label control in
We have a .aspx file which has about 400 lines of javascript code. Is
Im using aspxGridView control. It has templates inside. In aspx file I can write
I am trying to parse a file which has ITCH messages in it: http://www.nasdaqtrader.com/Trader.aspx?id=DPSpecs_USEquities#TVITCH
I would like to have this piece of code in my .aspx file: <input
If I paste something like <asp:DropDownList ID=DropDownListExpirationDate runat=server /> onto an ASPX file that
I have a page base class that has no .aspx file and so I
My application has a horizontal tab whose code is in the master file. Each
I have MyPage.aspx file and a button within. The button has server click event:
I've got an aspx file, and rendered within it is an ascx control. The

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.