简体   繁体   English

使用C#覆盖从Button扩展的控件的“文本”属性

[英]Overriding “Text” property of a control extended from Button using C#

I'm having a problem extending the standard WebControls.Button control. 我在扩展标准WebControls.Button控件时遇到问题。 I need to override the text property, but I receive the error message: 我需要覆盖text属性,但是会收到错误消息:

cannot override inhereted member 'System.Web.UI.WebControls.Button.Text.get' because it is not marked virtual, abstract or override 无法覆盖此处的成员'System.Web.UI.WebControls.Button.Text.get',因为未将其标记为虚拟,抽象或覆盖

I used the following code for a LinkButton, and that worked perfectly: 我为LinkBut​​ton使用了以下代码,并且效果很好:

public class IconLinkButton : LinkButton
{
    private string _icon = "";
    public string Icon
    {
        get
        {
            return _icon;
        }
        set
        {
            _icon = value;
        }
    }

    public override string Text
    {
        get
        {
            return "<i class=\""+Icon+"\"></i> " + base.Text;
        }
        set
        {
            base.Text = value;
        }
    }
}

However, doing the same thing for a standard Button kicks up the error I described above. 但是,对标准Button执行相同操作会引发我上面描述的错误。

public class IconButton : Button
{
    private string _icon = "";
    public string Icon
    {
        get
        {
            return _icon;
        }
        set
        {
            _icon = value;
        }
    }

    public virtual string Text
    {
        get
        {
            return "<i class=\"" + Icon + "\"></i> " + base.Text;
        }
        set
        {
            base.Text = value;
        }
    }
}

How can I fix this? 我怎样才能解决这个问题?

This is because LinkButton has a virtual Text property.. whilst Button does not. 这是因为LinkButton具有virtual Text属性。而Button没有。

You can hide the base functionality entirely by using new : 您可以使用new完全隐藏基本功能:

public class IconButton : Button {
    public new string Text {
        // implementation
    }
}

Using new hides the inherited member completely. 使用new完全隐藏继承的成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM