我有一个填充有整数的下拉列表。当选择“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;
}
}
默认.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>
最佳答案
您要添加autoPostBack="true"到下拉列表。
<asp:DropDownList ID="ddlHowMany" runat="server"
onselectedindexchanged="ddlHowMany_SelectedIndexChanged"
style="margin-left: 8px" Width="50px" AutoPostBack="true">
关于asp.net - 为什么此 ASP.Net 代码隐藏中的 if 语句不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15347715/