Change the SelectedItem value after selecing from a DropDownList and clicked button
Hi,
I have just started to use a ListItem[] in order to avoid using the collection property in the DropDownList so I have a list of items with their respective names and the values they should have behind them as follows:
private void InitializeDelivPointDropDown()
{
items[0] = new ListItem("Select", "-1");
items[1] = new ListItem("WareTest 1", "IPaddress");
items[2] = new ListItem("WareTest 2", "IPaddress");
items[3] = new ListItem("28A", "IPaddress");
items[4] = new ListItem( "26T","IPaddress");
items[5] = new ListItem("B9 2","IPaddress");
items[6] = new ListItem("B9 8","IPaddress");
items[7] = new ListItem("All Delivery Points", "0");
WorkcellDropDownList.Items.Clear();
WorkcellDropDownList.Items.AddRange(items);
}
now, when this code runs it initiates the value of the WorkcellDropDownList to be the first one.
So when running my scan button after I have selected a new index in the dropdownlist it still keeps the last values.
also I am running this code under the Page_Load but it seems that as soon as I click the button it runs the Page_Load event which is not what I would thought it would do, any way to change this?
Thanks!
Kirtan PatelPosted Dec 1, 2009, 7:17 AM
this is happening because when you select item from the DropDownList it PostBacks the page that Causes the page to
be reload ..and your drop down filled again by the items ..so your selected items get discarded
if you find my answer helpful then check "Do you like this answer check box please :)
here is solution ..
you should Write code like below to Fill the Combobox
protected void Page_Load(object sender, EventArgs e)
{
//this is postback is very important
if (!IsPostBack)
{
InitializeDelivPointDropDown();
}
}
private void InitializeDelivPointDropDown()
{
items[0] = new ListItem("Select", "-1");
items[1] = new ListItem("WareTest 1", "IPaddress");
items[2] = new ListItem("WareTest 2", "IPaddress");
items[3] = new ListItem("28A", "IPaddress");
items[4] = new ListItem("26T", "IPaddress");
items[5] = new ListItem("B9 2", "IPaddress");
items[6] = new ListItem("B9 8", "IPaddress");
items[7] = new ListItem("All Delivery Points", "0");
WorkcellDropDownList.Items.Clear();
WorkcellDropDownList.Items.AddRange(items);
}