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(Sorte dItem.ToSt ring());
}
}
Simplified source code is listed below. Thanks a lot!
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlProduct.Items.Insert(0,
}
foreach (string SortedItem in ItemList)
{
ddlProduct.Items.Add(Sorte
}
}
rebind the datasource to the dropdownlist
ASKER
How to rebind? I actually didnt use:
dropdownlist.dataSource = ArrayList;
dropdownlist.databind();
I used the above:
dropdownlist.Items.Add(Sor tedItem.To String());
Please advice. Thanks a lot.
dropdownlist.dataSource = ArrayList;
dropdownlist.databind();
I used the above:
dropdownlist.Items.Add(Sor
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["MyItem s"];
foreach( string mystring in myitems )
{
ddlProduct.Items.Add(mystr ing );
}
}
foreach (string SortedItem in ItemList)
{
ddlProduct.Items.Add(Sorte dItem.ToSt ring());
}
}
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.
example
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlProduct.Items.Insert(0,
ArrayList myItems = (ArrayList)Session["MyItem
foreach( string mystring in myitems )
{
ddlProduct.Items.Add(mystr
}
}
foreach (string SortedItem in ItemList)
{
ddlProduct.Items.Add(Sorte
}
}
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.
ASKER
cool, will try it tomorrow. thanks a lot.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
sorry, forgot to give out points. Thanks a lot.