簡體   English   中英

選擇下拉列表值時隱藏/顯示一些元素

[英]hide/show some elements when drop-down list value selected

所以,我有這些元素,標簽和下拉列表,我想根據其他下拉列表中的選定值來控制其可見性

主要的下拉列表是DropDownList1

<asp:DropDownList ID="DropDownList1" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
                        CssClass="dropdownRequestList">
                         <asp:ListItem>Teacher</asp:ListItem>
                         <asp:ListItem>Admin</asp:ListItem>
                    </asp:DropDownList> 

和其他元素需要控制其可見性Label9和DropDownList2

 <asp:Label ID="Label9" runat="server" Text="Department  :-" CssClass="lable" 
                        Visible="True"></asp:Label>
                </td>
                <td class="tx">
                    <asp:DropDownList ID="DropDownList2" runat="server" 
                        DataSourceID="SqlDataSource1" DataTextField="Department" 
                        DataValueField="Department" Visible="True" AutoPostBack="True">
                    </asp:DropDownList>

然后我編寫C#函數來控制可見性

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedItem.Text != "Teacher")
        {
            Label9.Visible = false;
            DropDownList2.Visible = false;
        }
    }

但是當選擇Admin時那些元素仍然可見,我還嘗試將這兩個元素的可見性初始化為false,然后在選擇Teacher值但頁面中沒有任何內容並且它們仍然隱藏時將它們設為true,這是怎么回事?

DropDownList1啟用回發功能,以便您的下拉菜單可以調用c#服務器端代碼。

<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
    CssClass="dropdownRequestList" AutoPostBack="True">
    <asp:ListItem>Teacher</asp:ListItem>
    <asp:ListItem>Admin</asp:ListItem>
</asp:DropDownList>

對於下面的c#代碼,請更新Page_Load的可見性,而不是DropDownList1_SelectedIndexChanged來處理以下所有情況。

protected void Page_Load(object sender, EventArgs e)
{
    if (DropDownList1.SelectedItem.Text != "Teacher")
    {
        Label9.Visible = false;
        DropDownList2.Visible = false;
    }
    else
    {
        Label9.Visible = true;
        DropDownList2.Visible = true;
    }
}

AutoPostBack="True"添加到DropDownList1

<asp:DropDownList ID="DropDownList1" runat="server"
 onselectedindexchanged="DropDownList1_SelectedIndexChanged"
 CssClass="dropdownRequestList" AutoPostBack="True">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM