2011-11-05 72 views
1

我在我的学校项目网站上创建注册表单。我给出了安全问题的下拉菜单,如果用户想输入他自己的问题,他/她可以选择其他问题,然后文本框会显示默认情况下我已经设置了visible=false如何显示隐藏的文本框下拉selectedindex chnged事件?

我试着用这段代码,但它不工作,有什么我失踪。

protected void selectques_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (selectques.Text == "Other") 
    { 
     alterquestion.Visible = true; 
    } 
} 

DROPDOWNLIST:

<asp:DropDownList ID="selectques" runat="server" Height="25px" Width="254px" OnSelectedIndexChanged="selectques_SelectedIndexChanged"> 
    <asp:ListItem>Select a question?</asp:ListItem> 
    <asp:ListItem> What is your pet name?</asp:ListItem> 
    <asp:ListItem>Who is your first teacher?</asp:ListItem> 
    <asp:ListItem>Which is your favourite movie?</asp:ListItem> 
    <asp:ListItem>Whom you like most in your life?</asp:ListItem> 
    <asp:ListItem>Other</asp:ListItem> 
</asp:DropDownList> 

隐藏的文本框:

<asp:TextBox ID="alterquestion" runat="server" Height="20px" Width="250px" Visible="false"></asp:TextBox> 

回答

2

DropDownList.Text房产给你SelectedValueDropDownList。但根据您的列表项目没有值字段

您需要检查selectques.SelectedItem.Text属性以查找您的ListItem或者您可以将值字段添加到您的列表项。

<asp:ListItem 
    Text="Other" 
    Value="Other" 
/> 

编辑

集的DropDownList的AutoPostBack为真

<asp:DropDownList ID="selectques" AutoPostBack= "True" runat="server" Height="25px" Width="254px" OnSelectedIndexChanged="selectques_SelectedIndexChanged"> 
+0

我试着用这两种方法,但仍是同样的结果。 – avirk

+0

有哟设置AutoPostBack =“真”? – Damith

+0

不,我没有。其实我不知道如何处理这个事件。 – avirk

2

你需要使用:

if (selectques.SelectedItem.Text == "Other") 

这应该做的伎俩