Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. DropDownList loses selection

DropDownList loses selection

Scheduled Pinned Locked Moved ASP.NET
helpdatabasesql-serversysadmin
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    laphijia
    wrote on last edited by
    #1

    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.

    P U 2 Replies Last reply
    0
    • L laphijia

      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.

      P Offline
      P Offline
      Paul Watson
      wrote on last edited by
      #2

      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 the Load event (or wherever PopulateDays 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...

      1 Reply Last reply
      0
      • L laphijia

        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.

        U Offline
        U Offline
        User 768313
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups