Asp.net下拉列表选择了项目值

时间:2015-08-26 06:46:18

标签: c# asp.net drop-down-menu

我对SelectedItem

中的DropDownList有疑问
<asp:DropDownList ID="Etkin_Drop" runat="server" OnSelectedIndexChanged="Etkin_Drop_SelectedIndexChanged">
     <asp:ListItem Text="Seç" Value="-1" Selected="True"></asp:ListItem>
     <asp:ListItem Text="Aktif" Value="1"></asp:ListItem>
     <asp:ListItem Text="Deaktif" Value="0"></asp:ListItem>
</asp:DropDownList>

第一个列表项值是-1但是当我想检查if语句时它不起作用

protected void Etkin_Drop_SelectedIndexChanged(object sender, EventArgs e)
{
    if (Convert.ToInt32(Etkin_Drop.SelectedItem.Value) == -1)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Lütfen Bir Seçim Yapınız');", true);
    }
    else
    {
        Label4.Text = Etkin_Drop.SelectedItem.Value;
    }
}

我无法定义问题

1 个答案:

答案 0 :(得分:4)

AutoPostBack属性添加到DropDownList并将此属性设置为True。像这样:

<asp:DropDownList ID="Etkin_Drop" runat="server" 
    OnSelectedIndexChanged="Etkin_Drop_SelectedIndexChanged" AutoPostBack="True">