DropDownList loses selection
-
I populated a DropDownList from SQL Server public void PopulateDays() { DataBaseAccessor dba = new DataBaseAccessor(); ddtFromDay.Items.Clear(); foreach(DataRow dr in dba.LoadHoursAndMinutes().Tables["HoursAndMinutes"].Rows) { ListItem li = new ListItem(dr["Hours"].ToString() + ":" + dr["Minutes"].ToString(), dr["ID"].ToString()); cbFromTime.Items.Add(li); cbToTime.Items.Add(li); } } Then I have a button, Add, that when clicked copies the current selection on the Drop Down List to a List box. private void btnAdd_Click(object sender, System.EventArgs e) { ListItem li; //Selected item from DropDownList li = ddlFromDay.SelectedItem; //Add item to ListBox lbDates.Items.Add(li); } The problem I face is that once the Add button is pressed the page posts back and I lose the selection on the Drop Down List therefore no matter what was previously the user selection it always adds the first item in the Drop Down List to the List Box. It seems that is not EnableViewState but indeed it is. I checked that already. I'm using cookieless StateSession and I tried to add the current selection to it but without any success. The real issue is that for me to perform ANY kind of action, even storing the current DropDownList.SelectedIndex to my StateSession, the page goes in post back and I lose the selection before I can memorize it. There must be something in how Drop Down Lists work that I'm not getting quite right. :zzz: Night coder: also today it's 6AM.
-
I populated a DropDownList from SQL Server public void PopulateDays() { DataBaseAccessor dba = new DataBaseAccessor(); ddtFromDay.Items.Clear(); foreach(DataRow dr in dba.LoadHoursAndMinutes().Tables["HoursAndMinutes"].Rows) { ListItem li = new ListItem(dr["Hours"].ToString() + ":" + dr["Minutes"].ToString(), dr["ID"].ToString()); cbFromTime.Items.Add(li); cbToTime.Items.Add(li); } } Then I have a button, Add, that when clicked copies the current selection on the Drop Down List to a List box. private void btnAdd_Click(object sender, System.EventArgs e) { ListItem li; //Selected item from DropDownList li = ddlFromDay.SelectedItem; //Add item to ListBox lbDates.Items.Add(li); } The problem I face is that once the Add button is pressed the page posts back and I lose the selection on the Drop Down List therefore no matter what was previously the user selection it always adds the first item in the Drop Down List to the List Box. It seems that is not EnableViewState but indeed it is. I checked that already. I'm using cookieless StateSession and I tried to add the current selection to it but without any success. The real issue is that for me to perform ANY kind of action, even storing the current DropDownList.SelectedIndex to my StateSession, the page goes in post back and I lose the selection before I can memorize it. There must be something in how Drop Down Lists work that I'm not getting quite right. :zzz: Night coder: also today it's 6AM.
You need to make sure you are not re-populating your drop-down on the post back. To check for this, use the
IsPostBack
property in theLoad
event (or whereverPopulateDays
is called from). regards, Paul Watson Bluegrass South Africa Brian Welsch wrote: "blah blah blah, maybe a potato?" while translating my Afrikaans. Crikey! ain't life grand? Einstein says... -
I populated a DropDownList from SQL Server public void PopulateDays() { DataBaseAccessor dba = new DataBaseAccessor(); ddtFromDay.Items.Clear(); foreach(DataRow dr in dba.LoadHoursAndMinutes().Tables["HoursAndMinutes"].Rows) { ListItem li = new ListItem(dr["Hours"].ToString() + ":" + dr["Minutes"].ToString(), dr["ID"].ToString()); cbFromTime.Items.Add(li); cbToTime.Items.Add(li); } } Then I have a button, Add, that when clicked copies the current selection on the Drop Down List to a List box. private void btnAdd_Click(object sender, System.EventArgs e) { ListItem li; //Selected item from DropDownList li = ddlFromDay.SelectedItem; //Add item to ListBox lbDates.Items.Add(li); } The problem I face is that once the Add button is pressed the page posts back and I lose the selection on the Drop Down List therefore no matter what was previously the user selection it always adds the first item in the Drop Down List to the List Box. It seems that is not EnableViewState but indeed it is. I checked that already. I'm using cookieless StateSession and I tried to add the current selection to it but without any success. The real issue is that for me to perform ANY kind of action, even storing the current DropDownList.SelectedIndex to my StateSession, the page goes in post back and I lose the selection before I can memorize it. There must be something in how Drop Down Lists work that I'm not getting quite right. :zzz: Night coder: also today it's 6AM.
hi you can solve it by using if not page.ispostback statement on page_load function. that's it i wish that this will help you.