Issues with DropDownList.Items.Addrange release and value selection

I am new to C # since I mainly used Java.

But straight to the problem (which I simplified a bit):

First excerpt of my aspx page:

<div class="dates">
    <div class="firstselection">
        <asp:DropDownList ID="DDFromMonth" runat="server">
        </asp:DropDownList>
        <asp:DropDownList ID="DDToMonth" runat="server">
        </asp:DropDownList>
    </div>
</div>

And codebehind:

    public partial class Argh: System.Web.UI.Page
    {
  static  List<ListItem> monthList = new List<ListItem>
  {
   new ListItem("Jan", "1"),
   new ListItem("Feb", "2"),
   new ListItem("Mar", "3"),
   new ListItem("Apr", "4"),
   new ListItem("May", "5"),
   new ListItem("Jun", "6"),
   new ListItem("Jul", "7"),
   new ListItem("Aug", "8"),
   new ListItem("Sep", "9"),
   new ListItem("Oct", "10"),
   new ListItem("Nov", "11"),
   new ListItem("Dec", "12")
  };



        protected void Page_Load(object sender, EventArgs e)
        {
   DDFromMonth.Items.AddRange(monthList.ToArray());  // DDFromMonth is a asp.net dropdown list
   DDToMonth.Items.AddRange(monthList.ToArray()); // DDToMonth is a asp.net dropdown list

            DDFromMonth.SelectedValue = "6"; 
            DDToMonth.SelectedValue = "7"; // also changes DDFromMonth value (GODDAMN!)

 }

Ok, I have a static list containing months. I am adding a range to this list for both the dropdown menu and DDFromMonth. I select "6" (June) and DDToMonth "7" (July). However, this leads to both dropdownlists showing July!

I also tried using a non-static list, but that doesn't matter. The only thing I tested, "works", is to create two different lists - only then the drop-down lists are selected as you wish. So, adding lists like this does the job (but ugly):

DDFromMonth.Items.AddRange(monthList.ToArray());
DDFromMonth.Items.AddRange(aDuplicatedMonthList.ToArray()); 

, asp.net, , , , . . , ? , -, -, # ASP.NET

+3
1

ListItems DropDownList. , 2 . , LAST, ListItem.

, , :

protected ListItem[] MonthList
{
   get
   {
      return new ListItem[]
      {
       new ListItem("Jan", "1"),
       new ListItem("Feb", "2"),
       new ListItem("Mar", "3"),
       new ListItem("Apr", "4"),
       new ListItem("May", "5"),
       new ListItem("Jun", "6"),
       new ListItem("Jul", "7"),
       new ListItem("Aug", "8"),
       new ListItem("Sep", "9"),
       new ListItem("Oct", "10"),
       new ListItem("Nov", "11"),
       new ListItem("Dec", "12")

       };
}

.

+3

Source: https://habr.com/ru/post/1757339/


All Articles