问题
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