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