Skip to main content
 首页 » 编程设计

c#之Web 用户控件设置默认值

2024年01月29日27mfrbuaa

如何设置Text的默认值为“abc”?

public enum MessageType 
{ 
    Good, 
    Bad 
} 
 
public partial class Message : System.Web.UI.UserControl 
{ 
    public bool Visible { get; set; }       // Is the error visible 
    public string Text { get; set; }        // Text of the error message 
    public MessageType Type { get; set; }   // Message type 
 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        ErrorPanel.Visible = this.Visible; 
        ErrorMsg.Text = this.Text; 
 
        // Hide if nothing to display 
        if (this.Text == null) 
                this.Visible = false; 
 
        // Set correct CSS class 
        if (this.Type == MessageType.Good) 
            ErrorPanel.CssClass = "good-box"; 
        else 
            ErrorPanel.CssClass = "bad-box"; 
    } 
} 

请您参考如下方法:

也许您可以使用声明属性的老派

private string _Text = "abc"; 
 
public string Text 
{ 
   get { return _Text; } 
   set { _Text = value; } 
}