Click to See Complete Forum and Search --> : dynamic generated DropDownList


csun
September 9th, 2004, 03:58 PM
Hello All,

I have a dynamic generated DropDownList in a table. The table has a couple of pages. When first time enters the Webpage (table is in the webpage). The ddl show items correctly. But when I went to another page and come back, I always got the first item of the ddl (data binded from database).

If I set AutoPostBack=true, I select item2, it will automatically return to item 1.

I have spent a couple of days on it. It seems hard.

Anyone can help me? Thanks.


Jen

repalley
September 9th, 2004, 05:39 PM
try this
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
fill ur dropdownlist
}
}

hope this helps

csun
September 10th, 2004, 11:40 AM
Thank you very much!

if(! Page.IsPostBack){
for (int i = 0; i < DataGrid1.Items.Count; i++) {
DropDownList ddl1 = (DropDownList)DataGrid1.Items[i].FindControl("DropDownList1");
ddl1.Items.Add("Item1");
ddl1.Items.Add("Item2");
ddl1.Items.Add("Item3");
ddl1.Items.Add("Item4");
ddl1.Items.Add("Item5");
ddl1.Items.Add("Item6");
//ddl1.Items[2].Selected = true;

}
}

Then only page1 in DataGrid1 has ddl filled. All ddl in other pages is empty.

Then

public void DataGrid1_OnPageIndexChanged(Object sender, DataGridPageChangedEventArgs e) {
Fill in ddl1 again
}

The same situation, either ddl1.AutoPostBack = true or NOT. I select item2 in first page(page1), then I go to other page, when I come back, the ddl1 in page1 does not show item2, but still item1 (which is default).

I guess I need to write ddl_OnSelectionChanged method and even to use an ArrayList to remember the selected value (ugly, remember Page_ID+DDL_ID+SelectedValue. When return to a page, look at the Page_ID, and DDL_ID, then using the SelectedValue for the ddl).

ANY suggestions are welcome. Thank you very much!

Chen