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. ListBox SelectedIndex does not persist during postback [modified]

ListBox SelectedIndex does not persist during postback [modified]

Scheduled Pinned Locked Moved ASP.NET
questioncsharpvisual-studiosysadminhelp
7 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.
  • T Offline
    T Offline
    Toni78
    wrote on last edited by
    #1

    I have created a custom user control using Visual Studio 2008. The user enters data in three TextBoxes. The data is added in a DataTable which is bound to a ListBox. The user has the option of deleting items from the ListBox. The ListBox is declared as follows:

    asp:ListBox ID="lstNames" runat="server" Height="99px" Width="245px"
    EnableViewState="True"
    onselectedindexchanged="lstNames_OnSelectedIndex"></asp:ListBox

    Inside the function Page_Load I have the following"

    if (!Page.IsPostBack)
    {
    ...
    lstNames.DataSource = table;
    lstNames.DataTextField = "Display";
    lstNames.DataBind();
    ...
    }

    I also have two buttons which allow me to add user input from TextBox controls to the ListBox and delete items. My question is: When I select an item and try to delete it, the SelecteIndex becomes 0 during the postback and the first item is removed (not the one I select). Basically, the SelectedIndex does not persist during a postback. I have looked at different postings here and other sites as well, searched MSDN but I cannot figure out the answer. Is there a way to overcome this problem and how? Thanks!

    Time is the fire in which we burn.

    J M 2 Replies Last reply
    0
    • T Toni78

      I have created a custom user control using Visual Studio 2008. The user enters data in three TextBoxes. The data is added in a DataTable which is bound to a ListBox. The user has the option of deleting items from the ListBox. The ListBox is declared as follows:

      asp:ListBox ID="lstNames" runat="server" Height="99px" Width="245px"
      EnableViewState="True"
      onselectedindexchanged="lstNames_OnSelectedIndex"></asp:ListBox

      Inside the function Page_Load I have the following"

      if (!Page.IsPostBack)
      {
      ...
      lstNames.DataSource = table;
      lstNames.DataTextField = "Display";
      lstNames.DataBind();
      ...
      }

      I also have two buttons which allow me to add user input from TextBox controls to the ListBox and delete items. My question is: When I select an item and try to delete it, the SelecteIndex becomes 0 during the postback and the first item is removed (not the one I select). Basically, the SelectedIndex does not persist during a postback. I have looked at different postings here and other sites as well, searched MSDN but I cannot figure out the answer. Is there a way to overcome this problem and how? Thanks!

      Time is the fire in which we burn.

      J Offline
      J Offline
      JHizzle
      wrote on last edited by
      #2

      Probably because you're messing with the listitem collection within the dropdown, you do that, it'll get confused because as far as it's concerned, it's a new datasource and therefore there's no selected. How are you marking which one is deleted in your codebehind? I'd probably create a seperate object to hold the data, use that as datasource for your dropdown and rebind each time otherwise there'll be a greater chance for state issues.

      T 1 Reply Last reply
      0
      • J JHizzle

        Probably because you're messing with the listitem collection within the dropdown, you do that, it'll get confused because as far as it's concerned, it's a new datasource and therefore there's no selected. How are you marking which one is deleted in your codebehind? I'd probably create a seperate object to hold the data, use that as datasource for your dropdown and rebind each time otherwise there'll be a greater chance for state issues.

        T Offline
        T Offline
        Toni78
        wrote on last edited by
        #3

        Thank you! That's interesting. Could you please elaborate a little bit? I rebind the datasource every time the page is loaded. Are you saying that this is considered as messing with the listitem collection? The code I use to delete the item is very simple (obviously I am trying to mark the item but I cannot):

        int iIndex = lstNames.SelectedIndex;

        if (iIndex > -1)
        {
        lstNames.Items.RemoveAt(iIndex);
        table.Rows.RemoveAt(iIndex);
        }

        Every time I change the selection, I would like to retrieve the selected index. I cannot because during postback it is set to 0. I have tried to save the view state, but that did not work either. My problem is that I cannot get the selected index at any time at all. If at any point I could retrieve its value, I can store it somewhere. The problem is that I am reading 0 at all times. I don't know what to do.

        Time is the fire in which we burn.

        J 1 Reply Last reply
        0
        • T Toni78

          Thank you! That's interesting. Could you please elaborate a little bit? I rebind the datasource every time the page is loaded. Are you saying that this is considered as messing with the listitem collection? The code I use to delete the item is very simple (obviously I am trying to mark the item but I cannot):

          int iIndex = lstNames.SelectedIndex;

          if (iIndex > -1)
          {
          lstNames.Items.RemoveAt(iIndex);
          table.Rows.RemoveAt(iIndex);
          }

          Every time I change the selection, I would like to retrieve the selected index. I cannot because during postback it is set to 0. I have tried to save the view state, but that did not work either. My problem is that I cannot get the selected index at any time at all. If at any point I could retrieve its value, I can store it somewhere. The problem is that I am reading 0 at all times. I don't know what to do.

          Time is the fire in which we burn.

          J Offline
          J Offline
          JHizzle
          wrote on last edited by
          #4

          Odd, I've used that code similarly myself and it's been fine unless the dropdown was added dynamically. Do you lose the selectedindex value if you remove the IsPostback check?

          T 1 Reply Last reply
          0
          • J JHizzle

            Odd, I've used that code similarly myself and it's been fine unless the dropdown was added dynamically. Do you lose the selectedindex value if you remove the IsPostback check?

            T Offline
            T Offline
            Toni78
            wrote on last edited by
            #5

            I lose the selectedIndex the moment I have some action that does a postback. I have tried to capture it in may places, during Page_Init, LoadViewState, SaveViewState, with the IsPostBack, and without it, etc. For the past two days, I have had no success. ...a little bit later... I am starting to believe that it is a data binding issue. You were absolutely correct. When I remove the databinding code, everything works fine. I need to figure out why.

            Time is the fire in which we burn.

            modified on Thursday, April 8, 2010 2:42 PM

            1 Reply Last reply
            0
            • T Toni78

              I have created a custom user control using Visual Studio 2008. The user enters data in three TextBoxes. The data is added in a DataTable which is bound to a ListBox. The user has the option of deleting items from the ListBox. The ListBox is declared as follows:

              asp:ListBox ID="lstNames" runat="server" Height="99px" Width="245px"
              EnableViewState="True"
              onselectedindexchanged="lstNames_OnSelectedIndex"></asp:ListBox

              Inside the function Page_Load I have the following"

              if (!Page.IsPostBack)
              {
              ...
              lstNames.DataSource = table;
              lstNames.DataTextField = "Display";
              lstNames.DataBind();
              ...
              }

              I also have two buttons which allow me to add user input from TextBox controls to the ListBox and delete items. My question is: When I select an item and try to delete it, the SelecteIndex becomes 0 during the postback and the first item is removed (not the one I select). Basically, the SelectedIndex does not persist during a postback. I have looked at different postings here and other sites as well, searched MSDN but I cannot figure out the answer. Is there a way to overcome this problem and how? Thanks!

              Time is the fire in which we burn.

              M Offline
              M Offline
              michaelschmitt
              wrote on last edited by
              #6

              Hi, are you sure, you dont rebind the datasource at any place on postbacks? I mean, obviously your items still appear in your listbox after postback, you would have mentioned it otherwise. So your control gets these items either from viewstate or by rebinding the datasource. If you rebind it on postback, the selectedindex is lost, but the items would still appear. I would suspect that in the first place, but in your code, i can see the "if (!ispostback...)" part. Hmm. Maybe you rebind it in some event? Good luck, these things can really make one scream.

              T 1 Reply Last reply
              0
              • M michaelschmitt

                Hi, are you sure, you dont rebind the datasource at any place on postbacks? I mean, obviously your items still appear in your listbox after postback, you would have mentioned it otherwise. So your control gets these items either from viewstate or by rebinding the datasource. If you rebind it on postback, the selectedindex is lost, but the items would still appear. I would suspect that in the first place, but in your code, i can see the "if (!ispostback...)" part. Hmm. Maybe you rebind it in some event? Good luck, these things can really make one scream.

                T Offline
                T Offline
                Toni78
                wrote on last edited by
                #7

                Thank you! You are absolutely correct (yes, I rebind the datasource). I was seriously about to lose my mind. It never occured to me that that could be an issue.

                Time is the fire in which we burn.

                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