Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表框所选索引相同值错误索引号_C#_Asp.net_Listbox_Listitem_Selectedindex - Fatal编程技术网

C# 列表框所选索引相同值错误索引号

C# 列表框所选索引相同值错误索引号,c#,asp.net,listbox,listitem,selectedindex,C#,Asp.net,Listbox,Listitem,Selectedindex,我在列表框中有相同的值。当我点击Index2(西班牙)时,它选择了Index0(美国) 我怎样才能通过这个错误 我必须在列表框或备用控件中使用相同的值 谢谢 这是我的密码 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ListItem li = new ListItem(); li.Text = "U

我在列表框中有相同的值。当我点击Index2(西班牙)时,它选择了Index0(美国) 我怎样才能通过这个错误

我必须在列表框或备用控件中使用相同的值

谢谢

这是我的密码

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListItem li = new ListItem();
            li.Text = "USA";
            li.Value = "06";
            ListItem li2 = new ListItem();
            li2.Text = "ITALY";
            li2.Value = "34";
            ListItem li3 = new ListItem();
            li3.Text = "SPAIN";
            li3.Value = "06";
            ListBox1.Items.Add(li);
            ListBox1.Items.Add(li2);
            ListBox1.Items.Add(li3);

        }

    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Write(ListBox1.SelectedIndex);
    }

因此,这个问题是由美国和西班牙包含相同的价值这一事实引起的。ASP.NET实际上不会获取列表框中已单击项的选定索引。相反,它使用您选择的值来确定索引。因为美国和西班牙有相同的值,所以它选择了第一个包含该值的索引

我将在页面上放置一个标签,并将其可见性设置为false,而不是使用Response.Write来获取ListItem的选定索引

然后,在SelectedIndexChanged事件中,将标签文本设置为列表框的选定索引

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    Label1.Text = ListBox1.SelectedIndex.ToString();
}

如果我是你,我会写这样的代码:

        if (!IsPostBack)
    {
        ListItem li = new ListItem();
        li.Text = "(06) USA";
        li.Value = "01";
        ListItem li2 = new ListItem();
        li2.Text = "(34) ITALY";
        li2.Value = "02";
        ListItem li3 = new ListItem();
        li3.Text = "(06) SPAIN";
        li3.Value = "03";
        ListBox1.Items.Add(li);
        ListBox1.Items.Add(li2);
        ListBox1.Items.Add(li3);

    }
然后我会得到括号部分作为值


PS:我注意到,如果列表框中有相同的文本,它将返回最小的
SelectedIndex
值(例如“John Doe”、“Jane Doe”、“Mr.Smith”、“Jane Doe”。在这种情况下,尽管您单击第二个Jane Doe,但它将返回1。John Doe=0,Jane Doe=1,Mr.Smith=2,Jane Doe=3)。所以这个答案也将是这个问题的解决方案。你可以写:“约翰·多伊”,“简·多伊(1)”,“史密斯先生”,“简·多伊(2)”或“约翰·多伊(0)”,“简·多伊(1)”,“史密斯先生(2)”,“简·多伊(3)”。

这是答案。写吧。您需要使用它吗?如果您只需要Response.Write方法,您可以尝试使用@Kramb的答案。但如果你需要任何逻辑,我认为最好的解决方案是使用不同的值。e、 即使您将其值保存到数据库中,您自己也无法解决它,是美国还是西班牙。因此,我建议您使用不同的值。声明OP必须使用相同的值。我认为这不起作用。我遇到了一个问题,在
列表框中有重复的条目
,当单击当前所选项目的副本时,我没有得到
SelectedIndexChanged
。似乎没有一种变通方法允许您具有重复的值。