Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.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# 为什么';dropdownlist是否选择了交换工作?_C#_Asp.net_Html Select - Fatal编程技术网

C# 为什么';dropdownlist是否选择了交换工作?

C# 为什么';dropdownlist是否选择了交换工作?,c#,asp.net,html-select,C#,Asp.net,Html Select,我有一个下拉列表,其中我添加了3项。我希望当第一项被选中时,标签的文本会改变。。。但它不起作用! 代码如下: protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList dr = new DropDownList(); if (dr.SelectedIndex == 1) { Label1.

我有一个下拉列表,其中我添加了3项。我希望当第一项被选中时,标签的文本会改变。。。但它不起作用! 代码如下:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dr = new DropDownList();
        if (dr.SelectedIndex == 1)
        {

            Label1.Text = "Good";
        }
        else if (dr.SelectedIndex == 2)
        {

            Label1.Text = "Bad";
        }

    }
首先,我在UpdatePanel中添加了dropdownlist,但它不起作用,所以我想可能是UpdatePanel让我遇到了这个问题。
我删除了更新面板,但它不工作

创建dropdownlist的新实例时,您必须使用屏幕上存在的此下拉列表


检查下拉列表的属性AutoPostBack是否设置为true。

尝试修改这行代码:

DropDownList dr = (DropDownList)sender;

当然,对于dropdownlist,将属性AutoPosback设置为true。

如果在selectedindexChanged事件中使用该属性,则无需进一步初始化它

直接使用:

if (DropDownList1.SelectedIndex == 1)
    {

        Label1.Text = "Good";
    }
    else if (DropDownList1.SelectedIndex == 2)
    {

        Label1.Text = "Bad";
    }

}

在您的aspx页面上,将下拉菜单的AutoPosback属性设置为true。这将解决您的问题。

dropdownlist的第一项索引从
0开始
我认为前面的答案中只有一部分在上下文中有用,即:如果下拉列表中没有任何项,则其selectedindex将为-1。