Link to home
Create AccountLog in
Avatar of heyday2004
heyday2004

asked on

Drop-Down list doesnt show the newly added item?

I have a dropdown list, when adding an item to the dropdown list and saved it, it didnt show up immediately in the dropdown list. No matter when I refresh the page, or select different item in the dropdown list, (these actions cause postbacks), but the newly added item still doesnt show up in the dropdown list. But after I close the window, click Ctrl+F5(start without debugging), the newly added item will show up in the dropdown list. Anybody can give me some hint on how the newly added item can be showed up in the dropdown list after I saved it to the database?
 
Simplified source code is listed below. Thanks a lot!  
 
protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
            ddlProduct.Items.Insert(0, "Please select  -----");
        }
 
  foreach (string SortedItem in ItemList)
        {
            ddlProduct.Items.Add(SortedItem.ToString());
        }
}
 
 
Avatar of KarinLoos
KarinLoos

rebind the datasource to the dropdownlist
Avatar of heyday2004

ASKER

How to rebind? I actually didnt use:
dropdownlist.dataSource = ArrayList;
dropdownlist.databind();

I used the above:
 dropdownlist.Items.Add(SortedItem.ToString());

Please advice. Thanks a lot.
Well you will have to persist the data in the dropdownlist  possibly in an arraylist which is then placed in the ssion as this will otherwise be gone once the page is posted back. Then in the page_load check for IsPostback and if its true,  retrieve the arraylist from the session and bind to the dropdownlist or loop data into the dropdowlist from the arraylist. . Alternatively as you are updating the database, you can also on postback retrieve the data from the datasource (DB) and bind to the dropdownlist.

example
protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
            ddlProduct.Items.Insert(0, "Please select  -----");
            ArrayList myItems = (ArrayList)Session["MyItems"];
            foreach( string mystring in myitems )
             {
                  ddlProduct.Items.Add(mystring );
             }
        }
 
  foreach (string SortedItem in ItemList)
        {
            ddlProduct.Items.Add(SortedItem.ToString());
        }
}
and on button click event prior or just after saving to DB  push the items in the dropdownlist to an arraylist and place in session.
cool, will try it tomorrow. thanks a lot.
ASKER CERTIFIED SOLUTION
Avatar of KarinLoos
KarinLoos

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
sorry, forgot to give out points. Thanks a lot.