【问题标题】:Is it possible to set default value for asp:Label?是否可以为 asp:Label 设置默认值?
【发布时间】:2014-10-15 05:00:12
【问题描述】:

TemplateField 中有更多标签位于 GridView 中。例如,此标签显示数据库中的每条记录:

<asp:Label runat="server" ID="lblName" Text='<%#: Item.Name %>'></asp:Label>

但有时,列名称没有价值,我想显示 - 而不是 String.Empty

我可以为它写一些函数,但是有很多列,如果可能的话,我宁愿在一个地方解决这个问题。所以我问是否有可能更改/覆盖/设置asp:Label 的默认值?

【问题讨论】:

    标签: c# asp.net label default-value


    【解决方案1】:

    你可以使用条件来做到这一点:

    <%#: !String.IsNullOrEmpty(Item.Name)?Item.Name:"-" %>
    

    或者您也可以从 System.Web.UI.WebControls.Label 类继承并创建自己的控件然后使用它。

    [Bindable(true), DefaultValue("-"), Localizable(true), PersistenceMode(PersistenceMode.InnerDefaultProperty), WebCategory("Appearance"), WebSysDescription("Label_Text")]
            public virtual string Text
    

    Text 属性是公共和虚拟的,您可以轻松地覆盖它。

    【讨论】:

    • 是的,这就是我的功能,但我想知道是否有办法覆盖asp:Label的默认值,即默认为String.Empty
    猜你喜欢
    • 2019-08-24
    • 2021-07-22
    • 1970-01-01
    • 2017-06-09
    • 2019-04-05
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 2011-10-15
    相关资源
    最近更新 更多