为什么这个ASP.Net Code-behind中的if语句不起作用?

Mon*_*ica 4 asp.net controls drop-down-menu

我有一个填充了整数的下拉列表.选择"1"时,应显示文本框.我在另一个项目中做了类似的事情,但在这里不起作用.

任何帮助表示赞赏.

C#代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ddlHowMany.Items.Add(new ListItem("0", "0"));
        ddlHowMany.Items.Add(new ListItem("1", "1"));
        ddlHowMany.Items.Add(new ListItem("2", "2"));
        ddlHowMany.Items.Add(new ListItem("3", "3"));
    }
}

protected void ddlHowMany_SelectedIndexChanged(object sender, EventArgs e)
{
    // int howMany = Convert.ToInt32(ddlHowMany.SelectedValue);
    if (ddlHowMany.SelectedIndex == 1)
    {
        txtGraphic1Desc.Visible = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

Default.aspx代码:

 <asp:DropDownList ID="ddlHowMany" runat="server"  
                    onselectedindexchanged="ddlHowMany_SelectedIndexChanged" 
                    style="margin-left: 8px" Width="50px">
                </asp:DropDownList>
                <br />
                <br />
                <asp:TextBox ID="txtGraphic1Desc" class="descriptions" runat="server" Height="92px" 
                    TextMode="MultiLine"
                    Width="328px" Font-Names="Trebuchet MS" Visible="False">Description of graphic #1</asp:TextBox>
Run Code Online (Sandbox Code Playgroud)

Win*_*Win 7

您想将autoPostBack ="true"添加到下拉列表中.

 <asp:DropDownList ID="ddlHowMany" runat="server"  
                    onselectedindexchanged="ddlHowMany_SelectedIndexChanged" 
                    style="margin-left: 8px" Width="50px" AutoPostBack="true">
Run Code Online (Sandbox Code Playgroud)