简体   繁体   English

带有SelectedIndex和Value的Listbox Bug?

[英]Listbox Bug with SelectedIndex and Value?

I think I found a bug in the use of the Listbox. 我想我在使用Listbox时发现了一个错误。 Example: ListBox with Editbutton which loads it into special fields: 示例:带有编辑按钮的ListBox将其加载到特殊字段中:

The listbox has 4 Items: 列表框有4个项目:

  1. PhoneNumber: +15454545 (Value 2)(Index 0) 电话号码:+15454545(值2)(索引0)
  2. Email: Test@testmail.com (Value 1)(Index 1) 电子邮件:Test@testmail.com(价值1)(索引1)
  3. FaxNumber: +1515515151 (Value 3)(Index 2) FaxNumber:+1515515151(Value 3)(Index 2)
  4. Email: Test2@testmail.com (Value 1)(Index 3) 电子邮件:Test2@testmail.com(价值1)(索引3)

Then the editbuttoncode: 然后是editbuttoncode:

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 items, its all fine. 当我选择前三项时,一切都很好。 When I select the 4th item, it uses all the data from the second one, even if they have different Indexes! 当我选择第4项时,它会使用第二项中的所有数据,即使它们具有不同的索引!

Does Value affect the Index here, and if yes, why ?! 价值是否会影响指数,如果是,为什么?! Its really a problem for me, because i need to store the contacttype in the Value. 它对我来说真的是一个问题,因为我需要在值中存储contacttype。 (1= Email, 2 = Phone etc); (1 =电子邮件,2 =电话等);

Thanks allready! 全部谢谢!

Edit: To clarify: The Edit-Button is a Button itself outside the ListBox. 编辑:澄清:编辑按钮是ListBox之外的Button本身。

<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 clarification(or whatever): Edit2以获得更多说明(或其他):

在此输入图像描述

This is how it looks. 这是它的外观。 Then i want to edit the 4th item: 然后我想编辑第4项:

在此输入图像描述

And when I debug: 当我调试时:

在此输入图像描述

The 4th Item is not selected ... even if you see in the picture befor, it is!! 没有选择第4项......即使你在图片中看到它,它也是!!

If I look at the ListBoxKOFC itself: 如果我查看ListBoxKOFC本身:

在此输入图像描述

And when the code is done, the wrong item got selected and loaded to edit: 代码完成后,选择并加载错误的项目进行编辑:

在此输入图像描述

But as you can see in the 4. Screenshot -> The index of the item i want to edit is three. 但正如您在4.屏幕截图中看到的那样 - >我要编辑的项目的索引是三个。 Only the value is the same. 只有价值是一样的。 But WHY cant the value be the same, the index is another allways ... 但为什么价值不一样,指数又是另一个......

Does the Value work as Index here ?! 这里的价值作为指数吗?! (Cant be, but atm im not sure anymore ...) Or is the Value just bugged ? (不可以,但我现在还不确定......)或者价值只是被窃听?

Yep. 是的。 Your problem is having two items with the same Value. 您的问题是有两个具有相同值的项目。 The ASP.Net dropdown renders to the browser as a standard HTML select control, with the form: ASP.Net下拉列表作为标准HTML选择控件呈现给浏览器,格式如下:

<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 http://www.w3schools.com/html/tryit.asp?filename=tryhtml_select2

Each option is supposed to have a unique value. 每个选项都应该具有唯一值。

Remember that when the postback happens, the ASP.Net engine creates a new instance of your page and applies the view state that came in with the request. 请记住,当回发发生时,ASP.Net引擎会创建页面的新实例并应用请求中带来的视图状态。 When it tries to re-create your dropdown control, it searches for an option with the correct value and stops when it finds one. 当它尝试重新创建下拉控件时,它会搜索具有正确值的选项,并在找到时停止。

I think I once had this problem before. 我想我曾经遇到过这个问题。 It turned out that you cannot set ListBox sort property to true; 事实证明,您无法将ListBox排序属性设置为true; otherwise the index will be useless. 否则索引将毫无用处。

But this might not be your case. 但这可能不是你的情况。

First of all, you should not have 2 item with same value in a list box. 首先,您不应该在列表框中有2个具有相同值的项目。 If you must have duplicate value then you can't use value as condition. 如果必须具有重复值,则不能将value用作条件。

Asp .Net does not treat the Value work as Index. Asp .Net不会将Value工作视为索引。 It is your code that use value as condition which is wrong(refer below). 您的代码使用值作为错误的条件(参见下文)。

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

Solution: You need to use index as your condition. 解决方案:您需要使用索引作为条件。 Try below code: 试试以下代码:

for (int i = 0; i < DropDownListKOFC.Items.Count; i++)
{
    DropDownListKOFC.Items[i].Selected = false;

    if (i == ListBoxKOFC.SelectedIndex)
    {
      DropDownListKOFC.Items[i].Selected = true;
    }
}

Thanks to the others for helping me to understand the problem! 感谢其他人帮助我理解这个问题! I will just share my little workarround, maybe it will help someone! 我将分享我的小工作,也许它会帮助别人!

Add a random value to the current value while you load it: 在加载时将随机值添加到当前值:

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

Then add it togheter with the value: 然后使用值将其添加到togheter:

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

And if you need it, just use substring: 如果你需要它,只需使用substring:

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

But this will only work if you have allways the same number of numbers infront of the random. 但是,只有当你总是在随机数量前面有相同数量的数字时,这才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM