【问题标题】:DropDownButton conditional statement with attributes?带有属性的 DropDownButton 条件语句?
【发布时间】:2020-02-24 21:56:01
【问题描述】:

我不知道如何检查我的 DropDownList 是否具有属性 Disabled

这是我的代码(关于我如何声明 DropDownList):

<div class="col-7">
    <asp:DropDownList ID="cmbProperty" runat="server" class="browser-default z-depth-5">
    </asp:DropDownList>
</div>

页面加载时:

protected void Page_Load(object sender, EventArgs e)
{
    cmbProperty.Attributes.Add("disabled", "disabled");
}

点击按钮:

protected void btnCheckMyProperty_Click(object sender, EventArgs e)
{
    if(cmbProperty.Enabled == true)
    {
        // I always get a true statement
    }            
}

有人知道吗?

谢谢

【问题讨论】:

  • 设置cmbProperty.Enabled = false 而不是disabled 属性有什么问题?
  • 它消失了。如果我这样做 cmbProperty.Enabled = false,DropDownList 就会消失(这可能是因为我的 css 设置),如果你喜欢我可以附上它的图片。

标签: asp.net if-statement drop-down-menu conditional-statements


【解决方案1】:

由于您评论说设置cmbProperty.Enabled = false 与您的css 混淆,您应该检查按钮单击事件中的disabled 属性而不是Enabled 属性。这很简单:

protected void btnCheckMyProperty_Click(object sender, EventArgs e)
{
    if(cmbProperty.Attributes["disabled"] == "disabled")
    {
        // Your code here...
    }            
}

注意:如果未设置 disabled 属性,则不会出错。在这种情况下它将返回 false...

【讨论】:

  • @ChangeWorld 这就是编码的魅力之一……还有无穷无尽的学习要做:) 很高兴我能帮到你。
猜你喜欢
  • 2013-06-01
  • 2021-11-13
  • 1970-01-01
  • 1970-01-01
  • 2021-06-19
  • 2019-06-12
  • 2020-08-18
  • 2017-12-02
  • 2017-03-06
相关资源
最近更新 更多