Using binded drop downs, value falling through others
I have three drop downs, and they all bind to the same data, now I am trying to select the drop down values when the client comes back to the last settings they stored. They will map 开发者_运维百科to the ones in the drop downs. The problem I am having is that whatever value the last drop down is taking.. is defaulting to the other ones although I am setting dropdown 1 with a value, drop down 2 with a value and drop down 3 with a different value... but all are selected to the value of the 3rd drop down.
var item1 = ddlMyItem1.Items.FindByValue("1");
if(item1!=null)
{
ddlMyItem1.ClearSelection();
item1.Selected = true;
}
var item2 = ddlMyItem2.Items.FindByValue("2");
if (item2 != null)
{
ddlMyItem2.ClearSelection();
item2.Selected = true;
}
var item3 = ddlMyItem3.Items.FindByValue("3");
if (item3 != null)
{
ddlMyItem3.ClearSelection();
item3.Selected = true;
}
For all drop downs it is defaulting to the value of the last one.. Any ideas why? or how i can fix this.. Thank you.
I think this is related to your databinding.
I may be off, but if you're databinding like this:
foreach(var item in myListItems)
ddlMyItem1.Items.Add(item)
foreach(var item in myListItems)
ddlMyItem2.Items.Add(item)
foreach(var item in myListItems)
ddlMyItem3.Items.Add(item)
The reference to each list item is the same for all your drop downs. Instead of manually building the Items collection; you're better off using the DataSource
property. The will create unique references; so something like this:
var list = new ListItemCollection {new ListItem("1", "1"), new ListItem("2", "2"), new ListItem("3", "3")};
DropDownList1.DataSource = list;
DropDownList1.DataBind();
DropDownList2.DataSource = list;
DropDownList2.DataBind();
DropDownList3.DataSource = list;
DropDownList3.DataBind();
var item = DropDownList3.Items.FindByValue("3");
item.Selected = true;
EDIT:
OK; here's what I would do to try and get this cleaned up:
1.) Create a static method that will return a collection of ListItems. This is what you're going to bind your DropDownLists to.
2.) Bind that collection of ListItems to the DataSource of each drop down; and then call DataBind().
3.) Pre-set each DropDownList with the saved values for the user.
Using an ObjectDataSource is also a valid way to go about this; but the above should get you going in the right direction. DO NOT add to the DropDownList Items collection; as that's what's getting you into trouble in the first place. Hope this helps.
You should refactor your code. We can help you solve your problem, but your foundation is not very solid.
Only call your
BindMyItems()
whenPage.IsNotPostback()
Inside
BindMyItems
, bind all 3 DDL to that dataview you have. After you.DataBind()
it, then insert your default value using.InsertAt(0, liDefault)
.The first two steps will always take place; now if you want to load your values that you saved from the database, you create another function called
LoadDDLValues()
Inside
LoadDDLValues()
, you use that load logic you already have (it is fine). This function should only be called when you load the pages other values (usually it's only loaded once whenPage.IsNotPostback()
or when you refresh the data)
精彩评论