在我的项目中,我将源信息保存在下拉列表中。喜欢信息来源:在下拉列表中有三个项目网站,报纸和其他。如果用户选择其他项目,则只能看到其他文本框,否则应该是不可见的。为此,我已设置页面加载事件
lblother.visible = FALSE; txtother.visible = FALSE;
在Btnsubmit事件中,我写了这样的条件。 如果(dropdownlistinfo.selectedindex == 2) { lblother.visible = TRUE; txtother.visible = TRUE; } 但在我的情况下,我没有得到我的欲望输出。当我从drowdownlist中选择Others项时,它总是不可见的。请有人帮助我,我的错误在哪里?
谢谢, 萨米特
答案 0 :(得分:0)
我认为问题出在这里。
if (!IsPostBack)
{
lblother.visible = false;
txtother.visible = false;
}
答案 1 :(得分:0)
如果您设置默认列表项的Selected属性,则此方法将起作用。
<asp:DropDownList ID="DropDownList" runat="server">
<asp:ListItem Text="Website" Selected="True"></asp:ListItem>
<asp:ListItem Text="Newspaper"></asp:ListItem>
<asp:ListItem Text="Other"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblOther" runat="server" Text="Other"></asp:Label>
<asp:TextBox ID="txtOther" runat="server"></asp:TextBox>
隐藏页面加载事件中的控件。
protected void Page_Load(object sender, EventArgs e)
{
this.txtOther.Visible = false;
this.lblOther.Visible = false;
}
然后在按钮点击事件中显示控件。
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 2)
{
this.txtOther.Visible = true;
this.lblOther.Visible = true;
}
}