如果使用C#选择了dropdownlist值,则显示div [英] display div if dropdownlist value is selected with C#

查看:120
本文介绍了如果使用C#选择了dropdownlist值,则显示div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在下拉列表中选择了特殊的选项",我正在尝试使Div出现.无论我尝试什么,我都无法使其正常工作.我在这里尝试了其他页面,但似乎没有任何作用.

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.

我的C#代码

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

我的DropDownList:

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>

和我要显示/隐藏的div:

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.

推荐答案

您需要启用下拉列表的AutoPostBack以便在服务器端引发 OnSelectedIndexChanged 事件.

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">

并更改标记:可见是html元素的属性,因此您可以像这样直接使用它.

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>

这篇关于如果使用C#选择了dropdownlist值,则显示div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆