原創|其它|編輯:郝浩|2009-04-27 09:45:04.000|閱讀 707 次
概述:在編寫自定義控件時,需要為頁面開發人員提供設計器屬性,便于他們設定控件的樣式、外觀。并且在提供設計器屬性時,需要為這些設計器屬性提供便于閱讀的說明。使用設計器屬性注釋自定義控件的屬性,可以提供該項功能。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在上一節中,我們通過集成TableStyle完成了RegistControlStyle類。現在我們需要在RegisterControl中使用自定義的RegistControlStyle。
使用自定義樣式,首先復寫RegistControl的CreatedControlStyle()方法,代碼如下所示:
protected override Style CreateControlStyle()
{
return new RegisterControlStyle(this.ViewState);
}
為什么需要復寫CreatedControlStyle屬性呢?因為RegisteControl繼承與WebControl,所有擁有ControlStyle屬性。在WebControl基類中,ControlStyle屬性的源代碼如下所示:
private Style controlStyle;
public Style ControlStyle
{
get
{
if (this.controlStyle == null)
{
this.controlStyle= this.CreateControlStyle();
}
return this.controlStyle;
}
}
由于自定義控件的ControlStyle屬性是通過CreatedControlStyle方法創建的,所以我們需要復寫CreatedControlStyle方法,覆蓋原有的ControlStyle屬性。
現在還需要復寫AddAttributeToRender方法,代碼如下所示:
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Id,this.ClientID);
writer.AddAttribute(HtmlTextWriterAttribute.Accesskey,this.AccessKey);
if (!this.Enabled)
{
writer.AddAttribute(HtmlTextWriterAttribute.Disabled,"disabled");
}
writer.AddAttribute(HtmlTextWriterAttribute.Tabindex,this.TabIndex.ToString());
writer.AddAttribute(HtmlTextWriterAttribute.Title,this.ToolTip.ToString());
this.ControlStyle.AddAttributesToRender(writer,this);
IEnumerator item = this.Attributes.Keys.GetEnumerator();
while (item.MoveNext())
{
string key = item.Current.ToString();
writer.AddAttribute(key,this.Attributes[key]);
}
}
公開所有我們需要使用的自定義控件的樣式屬性,代碼如下所示:
public virtual BackImageRepeat BackImageRepeat
{
get
{
RegisterControlStyle rs = this.ControlStyle as RegisterControlStyle;
return rs.BackImageRepeat;
}
set
{
RegisterControlStyle rs = this.ControlStyle as RegisterControlStyle;
rs.BackImageRepeat = value;
}
}
public new BorderStyle BorderStyle
{
get { return (this.ControlStyle as RegisterControlStyle).BorderStyle; }
set { (this.ControlStyle as RegisterControlStyle).BorderStyle = value; }
}
public new System.Web.UI.WebControls.Unit BorderWidth
{
get { return (this.ControlStyle as RegisterControlStyle).BorderWidth; }
set { (this.ControlStyle as RegisterControlStyle).BorderWidth = value; }
}
public int CellPadding
{
get { return (this.ControlStyle as RegisterControlStyle).CellPadding; }
set { (this.ControlStyle as RegisterControlStyle).CellPadding = value; }
}
public int CellSpacing
{
get { return (this.ControlStyle as RegisterControlStyle).CellSpacing; }
set { (this.ControlStyle as RegisterControlStyle).CellSpacing = value; }
}
public string BackImageUrl
{
get { return (this.ControlStyle as RegisterControlStyle).BackImageUrl; }
set { (this.ControlStyle as RegisterControlStyle).BackImageUrl = value; }
}
注意:所有的自定義樣式都需要在自定義控件中進行公開,才能被設計器識別。特別需要注意的是,基于自定義樣式的屬性都要進行轉換并且來自于ControlStyle屬性。
修改RenderBeginTag()方法,以便使用所有我們公開的自定義控件樣式屬性。代碼如下所示:
public override void RenderBeginTag(HtmlTextWriter writer)
{
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.AddAttribute(HtmlTextWriterAttribute.Id,"RegisterTable");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width,"auto");
writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "auto");
writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor,System.Drawing.
ColorTranslator.ToHtml(this.BackColor));
writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor,
System.Drawing.ColorTranslator.ToHtml(this.BorderColor));
writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle,this.BorderStyle.ToString());
writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth,this.BorderWidth.ToString());
writer.AddAttribute("cellpadding",this.CellPadding.ToString());
writer.AddAttribute("cellspacing", this.CellSpacing.ToString());
writer.AddStyleAttribute(HtmlTextWriterStyle.FontFamily,this.ControlStyle.Font.Name);
writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage,
(this.ControlStyle as RegisterControlStyle).BackImageUrl);
writer.AddStyleAttribute(this.BackImageRepeat.ToString(),this.
BackImageRepeat.ToString());
}
現在我們重新生成解決方案,重新再頁面上拖入RegistControl,設定樣式屬性,效果如圖4.1所示:
圖4.1 使用自定樣式屬性控制自定義控件樣式
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:博客園