2011-03-25 206 views
2

我需要一些幫助。我正在構建一個定製的Web用戶控件,它將會選擇顏色。asp.net用戶控件默認屬性值

截至目前,它在頁面上有兩個[asp:textbox] es(foregroundColour和backgroundColour)。

我希望我的前景色默認爲「FFFFFF」,我的背景色默認爲「000000」,並且用戶可以通過標籤指定自己的初始值。

<ucFontChooser id="testchooser" runat="server" foregroundcolor="AABBCC" /> 

我有被捆綁到文本框的兩個屬性:

[DefaultValue("000000")] 
public string ForegroundColor { get { return foregroundColorSelectorHex.Text; } set { foregroundColorSelectorHex.Text = value; } } 
[DefaultValue("FFFFFF")] 
public string BackgroundColor { get { return backgroundColorSelectorHex.Text; } set { backgroundColorSelectorHex.Text = value; } } 

請注意,我知道,將默認值僅適用於Visual Studio的屬性窗口中,並不實際設置的任何值。

不過,我已經嘗試設置默認值由用戶控件的構造函數來做到:

ForegroundColor = "000000" 

這給了我一個例外,因爲在構造來看,foregroundColorSelectorHex是null

所以,如果我嘗試在的OnInit事件設置默認,或*的Page_Load *那麼它只是始終使用我設置的默認和「AABBCC」的設定屬性值將被覆蓋。

什麼是正確的方式做我想要什麼?

謝謝。

回答

2

許多調試後,我想出了一個解決方案,做的正是我需要的。

public override string ID 
{ 
    set 
    { 
     base.ID = value; 
     InitializeProperties(); 
    } 
} 

protected void InitializeProperties() 
{ 
    ForegroundColor = "000000"; 
    BackgroundColor = "ffffff"; 
    EnableBackgroundColor = false; 
    PrimaryFont = "Arial"; 
    SecondaryFont = "Helvetica"; 
    TertiaryFont = "sans-serif"; 
} 

.NET框架將調用mycontrol.ID =「bla」,這是當用戶控制子控件進入上下文。它被稱爲BEFORE包含控件/頁面讀取標籤屬性並設置屬性。

看起來很不舒服,但效果很好。

1

我認爲一個完整的答案在這裏回答太長了,它已經在MSDN上有記錄。

看到這個頁面:http://msdn.microsoft.com/en-us/library/ms972975.aspx

,搜索文本「添加屬性和方法到用戶控件」

(但無論如何閱讀整篇文章,以便您瞭解詳情。)

但是,從文章中的代碼,它應該如何返回默認值,如果該屬性爲空。假設您有一個名爲CategoryID的屬性,則應該將該屬性定義爲:

public int CategoryID 
{ 
    get 
    { 
     object o = ViewState["CategoryID"]; 
     if (o == null) 
     return 0; // return a default value 
     else 
     return (int) o; 
    } 
    set 
    { 
     ViewState["CategoryID"] = value; 
    } 
} 

您可以使用相同的模式來定義顏色。

0

不知道這會工作,但你可以做這樣的事情在你的Page_Load:

protected void Page_Load { 
    if (String.IsNullOrEmpty(ForegroundColor)) 
     ForegroundColor = "FFFFFF"; 
} 
+0

這是在頁面生命週期中賦值太晚的值。 – Matthew 2011-03-29 18:40:44

0

你應該初始化值時,它不回傳即

if(!Page.IsPostBack){ 
    //Initialize your values 
    ForegroundColor = "FFFFFF"; 

} 
0

你能不能在構造函數中創建您的foregroundColorSelectorHex和backgroundColorSelectorHex?

public string ForegroundColor { 
    get { return foregroundColorSelectorHex.Text; } 
    set { foregroundColorSelectorHex.Text = value; } 
} 

public string BackgroundColor { 
    get { return backgroundColorSelectorHex.Text; } 
    set { backgroundColorSelectorHex.Text = value; } 
} 

public FontChooser() { 
    foregroundColorSelectorHex = new ColorSelector(); 
    backgroundColorSelectorHex = new ColorSelector(); 

    ForegroundColor = "FFFFFF"; 
    BackgroundColor = "000000"; 
} 
+0

我試過了,但asp.net只是在調用構造函數後創建了一個新的ColorSelector(),所以它們都被一個新對象覆蓋。 – Matthew 2011-03-25 20:22:17

1

建議保持財產的價值在ViewState中,然後設置。文本中Page_PreRender,像這樣:

public string ForegroundColor 
{ 
    get 
    { 
     if (ViewState["forecolor"] == null) 
     { 
      ViewState["forecolor"] = "000000"; 
     } 
     return (string) ViewState["forecolor"]; 
    } 
    set 
     { 
      ViewState["forecolor"] = value; 
     } 
} 

protected override void OnInit(EventArgs e) 
{ 
    this.PreRender += Page_PreRender; 
    base.OnInit(e); 
} 

private void Page_PreRender(object sender, EventArgs e) 
{ 
    foregroundColorSelectorHex.Text = ForegroundColor; 
}