
Button1.BackColor = System.Drawing.Color.Red;
Button1.ControlStyle.BackColor = System.Drawing.Color.Red;
myStyle.BackColor = System.Drawing.Color.Red;
Button1.ApplyStyle(myStyle);
style="background-color: red"
WebControl.BackColor
Style.BackColor
namespace CustomComponents
{
[ToolboxData(@"<{0}:ImageLabel1
BackColor='Red'
runat='server'></{0}:ImageLabel1>")
]
public class ImageLabel1 : Label
{
public override string Text
{
get
{ return ViewState["Text"] != null ? (string)ViewState["Text"] : base.ID; }
set
{ ViewState["Text"] = value; }
}
public override System.Drawing.Color BackColor
{
get
{
return base.BackColor = System.Drawing.Color.Red;
}
set
{
base.BackColor = value;
}
}
}
}
namespace CustomComponents
{
public class ImageLabel2 : Label
{
[BrowsableAttribute(true)]
[DescriptionAttribute("背景")]
[CategoryAttribute("Appearance")]
public virtual String ImageUrl
{
get
{ return ViewState["imageUrl"] != null ? (string)ViewState["imageUrl"] : ""; }
set
{ ViewState["imageUrl"] = value; }
}
override protected void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, ImageUrl);
base.AddAttributesToRender(writer);
}
}
}
protected override Style CreateControlStyle()
{
return new Style(ViewState);
}
protected override Style CreateControlStyle()
{
return new TableStyle(ViewState);
}
namespace CustomComponents
{
public class ImageLabel3 : Label
{
protected override Style CreateControlStyle()
{
return new TableStyle(ViewState);
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
//默认label控件
TableStyle a = new TableStyle();
a.BackImageUrl = "images4.bmp";
a.BackColor = System.Drawing.Color.Red;
Label1.ApplyStyle(a);
//自定义控件
ImageLabel3_1.ApplyStyle(a);
}
public override Color BackColor
{
get
{
return ((Style)ControlStyle).BackColor;
}
set
{
((Style)ControlStyle).BackColor = value;
}
}
public virtual string BackImageUrl
{
get
{ return ((TableStyle)ControlStyle).BackImageUrl; }
set
{ ((TableStyle)ControlStyle).BackImageUrl = value; }
}
控件样式#region 控件样式
protected override Style CreateControlStyle()
{
return new TableStyle(ViewState);
}
[BrowsableAttribute(true)]
[DescriptionAttribute("网格线")]
[CategoryAttribute("Appearance")]
public virtual GridLines GridLines
{
get
{ return ((TableStyle)ControlStyle).GridLines; }
set
{ ((TableStyle)ControlStyle).GridLines = value; }
}
[BrowsableAttribute(true)]
[DescriptionAttribute("单元格间距")]
[CategoryAttribute("Appearance")]
public virtual int CellSpacing
{
get
{ return ((TableStyle)ControlStyle).CellSpacing; }
set
{ ((TableStyle)ControlStyle).CellSpacing = value; }
}
[BrowsableAttribute(true)]
[DescriptionAttribute("单元格边距")]
[CategoryAttribute("Appearance")]
public virtual int CellPadding
{
get
{ return ((TableStyle)ControlStyle).CellPadding; }
set
{ ((TableStyle)ControlStyle).CellPadding = value; }
}
[BrowsableAttribute(true)]
[DescriptionAttribute("表水平对齐")]
[CategoryAttribute("Appearance")]
public virtual HorizontalAlign HorizontalAlign
{
get
{ return ((TableStyle)ControlStyle).HorizontalAlign; }
set
{ ((TableStyle)ControlStyle).HorizontalAlign = value; }
}
[BrowsableAttribute(true)]
[DescriptionAttribute("表背景图片")]
[CategoryAttribute("Appearance")]
public virtual string BackImageUrl
{
get
{ return ((TableStyle)ControlStyle).BackImageUrl; }
set
{ ((TableStyle)ControlStyle).BackImageUrl = value; }
}
#endregion
<custom:CreditCardForm6 BackColor="Black" ForeColor="White" runat="server" ID="example"
Font-Bold="true" Font-Italic="true" GridLines="None" CellSpacing="5"
BackImageUrl="images4.bmp" Font-Size="Larger"
BorderColor="Yellow" BorderWidth="20px" BorderStyle="Ridge" HorizontalAlign="NotSet" EnableViewState="False" />
public class LabelStyle :Style
{
public LabelStyle()
{ }
public LabelStyle(StateBag viewState) : base(viewState)
{ }
public virtual String ImageUrl
{
get
{ return ViewState["imageUrl"] != null ? (string)ViewState["imageUrl"] : ""; }
set
{ ViewState["imageUrl"] = value; }
}
public override void AddAttributesToRender(HtmlTextWriter writer, WebControl owner)
{
writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, ImageUrl);
base.AddAttributesToRender(writer, owner);
}
}
public class ImageLabel4 : Label
{
protected override Style CreateControlStyle()
{
return new LabelStyle(ViewState);
}
[Bindable(true),
Category("Appearance"),
DefaultValue(""),
Description("背景图片")
]
public virtual String ImageUrl
{
get
{ return ((LabelStyle)ControlStyle).ImageUrl; }
set
{ ((LabelStyle)ControlStyle).ImageUrl = value; }
}
}
//判断视图状态是否为空
internal bool IsSet(string key)
{
return ViewState[key] != null;
}
/**//// <summary>
/// 是否定义样式元素
/// </summary>
public override bool IsEmpty
{
get
{
return base.IsEmpty && !IsSet("imageUrl");
}
}
LabelStyle a = new LabelStyle();
a.ImageUrl = "images4.bmp";
a.BackColor = System.Drawing.Color.Red;
ImageLabel4_1.ApplyStyle(a);
方法#region 方法

/**//// <summary>
/// 复制样式
/// </summary>
/// <param name=""></param>
public override void CopyFrom(Style s)
{
if (s == null)
return;
base.CopyFrom(s);
LabelStyle ls = s as LabelStyle;
if (ls == null || ls.IsEmpty)
return;
if (ls.IsSet("imageUrl"))
this.ImageUrl = ls.ImageUrl;
}
/**//// <summary>
/// 整合样式
/// </summary>
/// <param name="s"></param>
public override void MergeWith(Style s)
{
if (s == null)
return;
if (IsEmpty)
{
CopyFrom(s);
return;
}
LabelStyle ls = s as LabelStyle;
if (ls == null || ls.IsEmpty)
return;
if (ls.IsSet("imageUrl") && !IsSet("imageUrl"))
this.ImageUrl = ls.ImageUrl;
}
/**//// <summary>
/// 清除样式
/// </summary>
public override void Reset()
{
base.Reset();
if (IsEmpty)
return;
if (IsSet("imageUrl"))
ViewState.Remove("imageUrl");
}
#endregion
protected override void FillStyleAttributes(CssStyleCollection attributes, IUrlResolutionService urlResolver)
{
base.FillStyleAttributes(attributes, urlResolver);
attributes.Add(HtmlTextWriterStyle.BackgroundImage, ImageUrl);
}
上一篇asp.net控件开发基础(7)--复合控件概述-.NET博客-博客园
下一篇asp.net控件开发基础(5)--复杂属性、内嵌属性-.NET博客-博客园