asp.net DropDownList in sync

2019-07-22 12:32发布

问题:

I have three dropdowns for selecting a year inside a user control. On load I specify the selected value for one drop down. The other two drop downs are automatically getting assigned the same selected value. No matter which dropdown I assign a selected value to, they all three get the same selected value. I've tried recreating a new user control for the dropdowns but the same thing happens.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DropDownTest.ascx.cs" Inherits="WymanOrgManagement.DropDownTest" %>
<p>
    <asp:DropDownList ID="ddlStartDateYear" runat="server">
    </asp:DropDownList>
</p>
<p>
    <asp:DropDownList ID="ddlEndDateYear" runat="server">
    </asp:DropDownList>
</p>
<p>
    <asp:DropDownList ID="Year" runat="server"></asp:DropDownList>
</p>

Code Behind:

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

namespace WymanOrgManagement
{
    public partial class DropDownTest : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            populateDatesAndStates();
            ddlEndDateYear.SelectedIndex = 2;
        }

        protected void populateDatesAndStates()
        {
            ListItem init2 = new ListItem();
            init2.Text = init2.Value = "Year...";
            ddlStartDateYear.Items.Add(init2);
            ddlEndDateYear.Items.Add(init2);
            Year.Items.Add(init2);

            for (int i = DateTime.Now.Year - 1; i < DateTime.Now.Year + 10; i++)
            {
                ListItem item = new ListItem();
                item.Text = item.Value = i.ToString();
                ddlStartDateYear.Items.Add(item); ddlEndDateYear.Items.Add(item);
                Year.Items.Add(item);
            }
        }
    }
}

Can someone help me figure out why the three dropdowns seem to be in sync? Thanks!

回答1:

All three dropdowns share the same instances of ListItem. When you set it to be Selected in one place, it becomes selected in all 3 places.

ListItem item = new ListItem();
item.Text = item.Value = i.ToString();
ddlStartDateYear.Items.Add(item); 
ddlEndDateYear.Items.Add(item);
Year.Items.Add(item);

What you probably want to do is this

ddlStartDateYear.Items.Add( i.ToString() ); 
ddlEndDateYear.Items.Add( i.ToString() );
Year.Items.Add( i.ToString() );

This will create a new instance of ListItem (with the same values) to go into each list.



回答2:

The problem is that you're sharing a reference to the ListItem across lists. So when the .Selected property is set in one drop down, since it's shared, it's set across all of them. You can just add strings instead of bothering with ListItems and avoid the issue completely.

protected void populateDatesAndStates()
{
    ddlStartDateYear.Items.Add("Year...");
    ddlEndDateYear.Items.Add("Year...");
    Year.Items.Add("Year...");

    for (int i = DateTime.Now.Year - 1; i < DateTime.Now.Year + 10;  i++)
    {
        ddlStartDateYear.Items.Add(i.ToString());
        ddlEndDateYear.Items.Add(i.ToString());
        Year.Items.Add(i.ToString());
    }
}