Listbox error with SelectedIndex and value?

I think I found a mistake in using Listbox. Example: ListBox with an Editbutton button that loads it into special fields:

There are 4 elements in the list:

  • PhoneNumber: +15454545 (value 2) (index 0)
  • Email: Test@testmail.com (value 1) (index 1)
  • Fax: +1515515151 (value 3) (index 2)
  • Email: Test2@testmail.com (value 1) (index 3)

Then the editbuttoncode file:

protected void EditKOFC(object sender, EventArgs e) { try { if (ListBoxKOFC.SelectedItem == null) { LabelMPE.Text = "Please select first!"; ModalPopupExtender1.Show(); } else { string value = ListBoxKOFC.SelectedValue; Session["EditID"] = ListBoxKOFC.SelectedIndex; string[] meineStrings = ListBoxKOFC.SelectedItem.Text.Split(new Char[] { ':' }); string text = meineStrings[1]; string text2 = text.Substring(1); TextBoxKOFC.Text = text2; foreach (ListItem item in DropDownListKOFC.Items) { item.Selected = false; if (item.Value == value) { item.Selected = true; } } editing = true; AddKOFC.Text = "Save"; } } catch (Exception ex) { GlobalFunctions.Error_Log(ex, ex.TargetSite.ToString()); } } 

And there I get the problem. When I select the first three elements, everything is in order. When I select the 4th element, it uses all the data from the second, even if they have different indexes!

The value affects the index here, and if so, why ?! This is really a problem for me, because I need to store the contacttype in Value. (1 = email, 2 = phone, etc.);

Thanks allready!

Edit: To clarify: the "Edit" button is the button itself outside the ListBox.

 <asp:TableRow> <asp:TableCell> <asp:UpdatePanel runat="server" UpdateMode="Conditional" ID="UpdatePanel2"> <Triggers> <asp:AsyncPostBackTrigger ControlID="AddKOFC" EventName="Click" /> </Triggers> <ContentTemplate> <asp:DropDownList runat="server" ID="DropDownListKOFC" /> </ContentTemplate> </asp:UpdatePanel> </asp:TableCell> <asp:TableCell> <asp:UpdatePanel runat="server" UpdateMode="Conditional" ID="UpdatePanel1"> <Triggers> <asp:AsyncPostBackTrigger ControlID="AddKOFC" EventName="Click" /> </Triggers> <ContentTemplate> <asp:TextBox runat="server" ID="TextBoxKOFC" /> </ContentTemplate> </asp:UpdatePanel> </asp:TableCell><asp:TableCell> <asp:UpdatePanel runat="server" UpdateMode="Conditional" ID="UP2"> <ContentTemplate> <asp:Button runat="server" ID="AddKOFC" OnClick="AddContactInformation" Text="Add Contactinformation" /> <asp:HiddenField ID="HFAdd" runat="server" /> <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" BackgroundCssClass="modalBackground" TargetControlID="HFAdd" PopupControlID="PanelChoose" BehaviorID="MPEchoose"> </ajaxToolkit:ModalPopupExtender> <asp:Panel ID="PanelChoose" runat="server" BorderStyle="Solid" BackColor="ButtonShadow"> <asp:Label ID="LabelMPE" runat="server"></asp:Label> <asp:Table ID="Table3" runat="server"> <asp:TableRow> <asp:TableCell> <asp:Button ID="ButtonOK" runat="server" Text="Ok" /> </asp:TableCell> </asp:TableRow> </asp:Table> </asp:Panel> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="AddKOFC" EventName="Click" /> <asp:AsyncPostBackTrigger ControlID="ButtonOK" EventName="Click" /> </Triggers> </asp:UpdatePanel> </asp:TableCell></asp:TableRow> <asp:TableRow> <asp:TableCell> <asp:Label ID="Label13" runat="server"></asp:Label> </asp:TableCell></asp:TableRow> <asp:TableRow> <asp:TableCell> <asp:UpdatePanel runat="server" UpdateMode="Conditional" ID="UP3"> <Triggers> <asp:AsyncPostBackTrigger ControlID="AddKOFC" EventName="Click" /> </Triggers> <ContentTemplate> <asp:ListBox runat="server" ID="ListBoxKOFC" ToolTip="The way to contact this person"> </asp:ListBox> </ContentTemplate> </asp:UpdatePanel> </asp:TableCell><asp:TableCell> <asp:Button ID="ButtonUpdate" runat="server" Text="Edit" OnClick="EditKOFC" /> </asp:TableCell><asp:TableCell> <asp:Button ID="ButtonDelete" runat="server" Text="Delete" OnClick="DeleteKOFC" /> </asp:TableCell></asp:TableRow> <asp:TableRow> 

Edit2 for more info (or something else):

enter image description here

Here is how it looks. Then I want to edit the 4th element:

enter image description here

And when I debug:

enter image description here

The 4th element is not selected ... even if you see in the picture, because it is!

If I look at the ListBoxKOFC itself:

enter image description here

And when the code is executed, the wrong item was selected and loaded for editing:

enter image description here

But as you can see in 4. Screenshot β†’ The index of the item I want to change is three. Only one value. But WHY the same value cannot be, the index anyway ...

Does the value contain the Index value here ?! (I can’t, but atm im not sure more ...) Or is this value just listening?

+4
source share
4 answers

Yeah. There are two elements in your problem with the same value. The ASP.Net dropdown displays the browser as a standard HTML selection control with the form:

 <select name="cars"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="fiat">Fiat</option> <option value="audi">Audi</option> </select> 

http://www.w3schools.com/html/tryit.asp?filename=tryhtml_select2

Each option must have a unique value.

Remember that when the postback occurs, the ASP.Net engine creates a new instance of your page and applies the view state that is included in the request. When it tries to recreate your dropdown control, it searches for an option with the correct value and stops when it finds it.

+2
source

I think I had this problem before. It turned out that you cannot set the sort property of the ListBox to true; otherwise the index will be useless.

But that may not be your business.

+1
source

First of all, you should not have 2 items with the same value in the list. If you must have a duplicate value, you cannot use the value as a condition.

Asp.Net does not treat the value as an Index. This is your code that uses the value as a condition that is incorrect (see below).

 if (item.Value == value) { item.Selected = true; } 

Solution: You need to use the index as your condition. Try entering the code:

 for (int i = 0; i < DropDownListKOFC.Items.Count; i++) { DropDownListKOFC.Items[i].Selected = false; if (i == ListBoxKOFC.SelectedIndex) { DropDownListKOFC.Items[i].Selected = true; } } 
0
source

Thanks to others for helping to understand the problem! I'll just talk about my small workplace, maybe this will help someone!

Add a random value to the current value while it is loading:

 Random rnd = new Random(); int random = rnd.Next(1,1000000); 

Then add the togheter value with the value:

 ListItem listitem = new ListItem(text, value + random.ToString(), true); ListBox.Items.Add(listitem); 

And if you need it, just use the substring:

 string value = ListBox.SelectedValue; value = value.Substring(0, 1); 

But this will only work if you always have the same number of numbers in random order.

0
source

Source: https://habr.com/ru/post/1498344/


All Articles