2011-11-05 73 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") 

這應該做的伎倆