2011-04-11 133 views
1

如何設置文本的默認值爲「abc」?Web用戶控制設置默認值

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"; 
    } 
} 

回答

3

也許你可以使用聲明屬性

的老派
private string _Text = "abc"; 

public string Text 
{ 
    get { return _Text; } 
    set { _Text = value; } 
} 
3

你可以一個默認值屬性添加到房產嗎?

如:

[System.ComponentModel.DefaultValue("abc")] 
public string Text {get;set;} 
+0

這個工作嗎? – V4Vendetta 2011-04-11 09:49:05

+0

似乎對我從它複製的用戶控件起作用。在設計器中將值設置爲默認值,並且如果以粗體突出顯示更改 – WraithNath 2011-04-11 09:49:59

0

我相信這樣做會在構造方法的類的

OR

private string _Text;  
public string Text 
{ 
    get { return _Text ?? "Your Default"; } 
    set { _Text = value; } 
} 

編輯的最佳場所

DefaultValueAttribute不會導致使用該屬性的值自動初始化成員。您必須在代碼中設置初始值。

From MSDN