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):

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

And when I debug:

The 4th element is not selected ... even if you see in the picture, because it is!
If I look at the ListBoxKOFC itself:

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

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?