【问题标题】:Style.Add not working for display none/inline in asp.net code behindStyle.Add 无法在 asp.net 代码中显示无/内联
【发布时间】:2013-01-15 22:03:14
【问题描述】:

在单击按钮时,我试图将样式应用于两个按钮以从本质上交换它们 - 将一个设置为 display:inline,另一个设置为 display:none,但是,Control.Style.Add("display" ,"none") 和 Control.Style.Add("display","inline") 不起作用。

但是,如果我设置了任何其他样式设置,它就可以工作。 Control.Style.Add("background","#ff0000") 有效,Style.Remove("whatever") 和 Style.Clear()

我的功能是用户控件内的一个简单按钮:

Protected Sub SimpleButton_Click(sender As Object, e As System.EventArgs)
    Me.AnotherTextBox.Text = ""  ' WORKS!
    Me.SimpleButton.Style.Remove("display")
    Me.SimpleButton.Style.Add("display", "none")
    Me.SimpleButton.Style.Add("background", "#ff0000")  ' WORKS!
    Me.AnotherSimpleButton.Style.Remove("display")
    Me.AnotherSimpleButton.Style.Add("display", "inline")

End Sub

我的页面代码:

            <asp:LinkButton CausesValidation="false" Style="padding: 0px 4px; color: GrayText; font-weight: bold;" ID="AnotherSimpleButton" runat="server" Text="Add" Enabled="false"></asp:LinkButton>
           <asp:LinkButton CausesValidation="false" Style="padding: 0px 4px; color: Blue; font-weight: bold;display:none" ID="SimpleButton" runat="server" Text="Add" OnClick="SimpleButton_Click"></asp:LinkButton>

我尝试过使用和不使用上述 Style.Remove("display"),以及使用 Style.Clear() 并重建整个样式 - 仍然没有骰子。显示风格有什么特别之处吗?

【问题讨论】:

    标签: asp.net css vb.net


    【解决方案1】:

    我解决了自己的问题 - 当然是用户错误。与稍后的页面事件相关联的功能是向这些按钮添加“显示”样式,这就是为什么只有“显示”css 不起作用的原因——它只是被覆盖了。 (这是我忽略的一个主要线索……)

    【讨论】:

      【解决方案2】:

      第一行没有 Handles 语句,即它应该说:

      Protected Sub SimpleButton_Click(sender As Object, e As System.EventArgs) Handles SimpleButton.click
      

      可能是在 Page.Load 中设置了 AnotherTextBox.Text 和 Me.SimpleButton.Style.Add ,这就是它们似乎起作用的原因吗?

      【讨论】:

      • 在页面文件中,我在 控件中设置了 OnClick 属性。此外,在单击按钮时,背景颜色会从 page_load 的白色值变为红色。 (我最初可以处理它,但由于其他原因不得不切换到 OnClick。)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 2013-07-28
      • 2017-11-06
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多