I have a webpage with 4 dropdownlists on the page. In the page load method I have the code behind set the values of the dropdownlists. The problem is that when I set any one of the dropdownlists it sets all of the dropdownlists.
我有一个网页,上面有4个下拉列表。在page load方法中,我有设置下拉列表值的代码。问题是当我设置任何一个下拉列表时它会设置所有下拉列表。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//populating the dropdownlist with values
for (int i = 0; i < 60; i++)
{
ListItem temp = new ListItem(i + "");
ddl_EndMin.Items.Add(temp);
ddl_StartMin.Items.Add(temp);
if (i < 24)
{
ddl_EndHour.Items.Add(temp);
ddl_StartHour.Items.Add(temp);
}
}
//Setting the dropdownlists with the values from the conference variable
ddl_EndHour.SelectedIndex = conference.EndDate.Hour;
ddl_StartMin.SelectedIndex = conference.StartDate.Minute;
ddl_StartHour.SelectedIndex = conference.StartDate.Hour;
ddl_EndMin.SelectedIndex = conference.EndDate.Minute;
}
}
}
I'm not sure why setting one of these dropdownlists selected index sets all of them. I also tried replacing one of them with a ListBox and the value of the ListBox was set as well. There is code on another page that sets 2 dropdownlists using this selected index method but using states instead of numbers and that works just fine.
我不确定为什么要设置这些下拉列表中的一个索引设置它们。我还尝试用列表框替换其中的一个,并且设置了列表框的值。另一个页面上的代码使用这个选定的索引方法设置2个下拉列表,但是使用状态而不是数字,这很好。
ddl_EndMin.SelectedIndex = ddl_EndMin.Items.IndexOf(ddl_EndMin.Items.FindByValue(conference.EndDate.Minute.ToString()));
ddl_EndHour.SelectedIndex = ddl_EndHour.Items.IndexOf(ddl_EndHour.Items.FindByValue(conference.EndDate.Hour.ToString()));
I tried copy/pasting that code into what I'm currently working on and changing the names and I got the same results. Any insight you can give me as to why this problem is occuring would be greatly appreciated.
我尝试将代码复制/粘贴到我正在处理的代码中,并更改了名称,得到了相同的结果。如果您能告诉我为什么会出现这个问题,我将不胜感激。
1 个解决方案
#1
3
At a guess it's because you're using the same item collection in all of your dropdowns.
猜测是因为您在所有的下拉菜单中都使用了相同的项目集合。
Then when you set the selected
property on one of the items it has that property in all of your lists as it's the same object reference in all lists.
然后,当您在其中一个项目上设置选定属性时,它在所有列表中都有该属性,因为它在所有列表中都是相同的对象引用。
What happens if you do this within the loop
如果你在循环中这样做会发生什么?
ListItem temp = new ListItem(i + "");
ddl_EndMin.Items.Add(temp);
temp = new ListItem(i + "");
ddl_StartMin.Items.Add(temp);
if (i < 24)
{
temp = new ListItem(i + "");
ddl_EndHour.Items.Add(temp);
temp = new ListItem(i + "");
ddl_StartHour.Items.Add(temp);
}
#1
3
At a guess it's because you're using the same item collection in all of your dropdowns.
猜测是因为您在所有的下拉菜单中都使用了相同的项目集合。
Then when you set the selected
property on one of the items it has that property in all of your lists as it's the same object reference in all lists.
然后,当您在其中一个项目上设置选定属性时,它在所有列表中都有该属性,因为它在所有列表中都是相同的对象引用。
What happens if you do this within the loop
如果你在循环中这样做会发生什么?
ListItem temp = new ListItem(i + "");
ddl_EndMin.Items.Add(temp);
temp = new ListItem(i + "");
ddl_StartMin.Items.Add(temp);
if (i < 24)
{
temp = new ListItem(i + "");
ddl_EndHour.Items.Add(temp);
temp = new ListItem(i + "");
ddl_StartHour.Items.Add(temp);
}