display div if dropdownlist value is selected with C#

泪湿孤枕 提交于 2019-12-25 04:57:29

问题


I'm trying to make a Div appear if special "option" is selected in a Drop Down list. No matter what I try I can't make it work. I tried other pages here but nothing seems to make it work.

My C# code

protected void ddlSubject_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ddlEmne.SelectedValue == "Lej os")
    {
        divselected.Visible = true;
    }
    if (ddlEmne.SelectedValue == "")
    {
        divselected.Visible = false;
    }
}

My DropDownList:

<asp:DropDownList CssClass="margtop" ID="ddlEmne"  runat="server" OnSelectedIndexChanged="ddlSubject_SelectedIndexChanged">
                <asp:ListItem Value="Pakke Løsninger">Pakke Løsninger</asp:ListItem>
                <asp:ListItem Value="Spørgsmål">Spørgsmål</asp:ListItem>
                <asp:ListItem Value="Lej os">Lej os</asp:ListItem>
                <asp:ListItem Value="Andet">Andet</asp:ListItem>
            </asp:DropDownList>

and the div I want to show/hide :

<div id="divselected" runat="server" style="visibility: hidden;">
    s
</div>

Hope that someone can say that I'm doing wrong.


回答1:


You need to Enable the AutoPostBack of the dropdownlist for raising the OnSelectedIndexChanged event on server side.

asp:DropDownList CssClass="margtop" ID="ddlEmne" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlSubject_SelectedIndexChanged">

and change the markup: visible is the attribute of html elements, so you can directly use it like this.

 <div id="divselected" runat="server" visible="false">
    s
 </div>


来源:https://stackoverflow.com/questions/21068367/display-div-if-dropdownlist-value-is-selected-with-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!