| ASP.NET2.0服务器控件之类型化样式属性 |
|
| 来源:天极yesky 作者: 加入时间:2006-10-27 访问次数:4 [大 中 小] |
|
 图2 |
如上代码所示,MyPanel继承自WebControl基类,其中定义了3个属性BackImageUrl、HorizontalAlign和Wrap。关于这3个属性的说明,读者可参考前面的内容。另外,MyPanel重写了CreateControlStyle方法,返回一个MyPanelStyle对象。这样返回的MyPanelStyle实例间接的赋值给ControlStyle属性。这种实现方法的原因是由于ControlStyle属性是只读属性,且它的访问操作需要调用CreateControlStyle方法时间接进行设置。需要读者注意的是,CreateControlStyle将MyPanel控件的ViewState传递给MyPanelStyle的构造函数。当在CreateControlStyle中为控件创建新样式时,必须将控件的ViewState传给Style构造函数,那么样式对象则使用和控件相同的StateBag。
下面列举了实现MyPanelStyle类的源代码,它们来自MyPanelStyle.cs文件。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebControlLibrary{ public class MyPanelStyle : Style { // 定义内部常量 internal const int PROP_BACKIMAGEURL = 1; internal const int PROP_HORIZONTALALIGN = 2; internal const int PROP_WRAP = 3; //构造函数一 public MyPanelStyle() { } // 构造函数二 public MyPanelStyle(StateBag bag) : base(bag) { } // 创建BackImageUrl属性 [ Bindable(true), Category("Appearance"), DefaultValue(""), Description("背景图片的URL"), NotifyParentProperty(true) ] public virtual string BackImageUrl { get { if (IsSet(PROP_BACKIMAGEURL)) { return (string)ViewState["BackImageUrl"]; } return String.Empty; } set { ViewState["BackImageUrl"] = value; } } // 实现HorizonalAlign属性 [ Bindable(true), Category("Layout"), DefaultValue(HorizontalAlign.NotSet), Description("所添加内容的水平对其方式"), NotifyParentProperty(true) ] public virtual HorizontalAlign HorizonalAlign { get { if (IsSet(PROP_HORIZONTALALIGN)) { return (HorizontalAlign)ViewState["HorizontalAlign"]; } return HorizontalAlign.NotSet; } set { if (value < HorizontalAlign.NotSet || value > HorizontalAlign.Justify) { throw new ArgumentOutOfRangeException("value"); } ViewState["HorizontalAlign"] = value; } } // 实现IsEmpty protected new internal bool IsEmpty { get { return base.IsEmpty && !IsSet(PROP_BACKIMAGEURL) && !IsSet(PROP_HORIZONTALALIGN) && !IsSet(PROP_WRAP); } } //实现Wrap属性 [ Bindable(true), Category("Layout"), DefaultValue(true), Description("是否允许对所添加的内容换行"), NotifyParentProperty(true) ] public virtual bool Wrap { get { if (IsSet(PROP_WRAP)) { return (bool)ViewState["Wrap"]; } return true; } set { ViewState["Wrap"] = value; } } //辅助方法IsSet internal bool IsSet(int propNumber) { string key = null; switch (propNumber) { case PROP_BACKIMAGEURL: key = "BackImageUrl"; break; case PROP_HORIZONTALALIGN: key = "HorizontalAlign"; break; case PROP_WRAP: key = "Wrap"; break; } if (key != null) { return ViewState[key] != null; } return false; } //重写AddAttributesToRender方法 public override void AddAttributesToRender(HtmlTextWriter writer, WebControl owner) { if (IsSet(PROP_BACKIMAGEURL)) { string s = BackImageUrl; if (s.Length > 0) { if (owner != null) { s = owner.ResolveUrl(s); } writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, "url(" + s + ")"); } } if (IsSet(PROP_HORIZONTALALIGN)) { System.Web.UI.WebControls.HorizontalAlign hAlign = this.HorizonalAlign; if (hAlign != System.Web.UI.WebControls.HorizontalAlign.NotSet) { TypeConverter hac = TypeDescriptor.GetConverter(typeof(HorizontalAlign)); writer.AddAttribute(HtmlTextWriterAttribute.Align, hac.ConvertToInvariantString(hAlign)); } } if (IsSet(PROP_WRAP)) { bool wrap = Wrap; if (!Wrap) { writer.AddAttribute(HtmlTextWriterAttribute.Nowrap, "nowwrap"); } } base.AddAttributesToRender(writer, owner); } //重写CopyFrom方法 public override void CopyFrom(Style s) { if (s != null) { base.CopyFrom(s); if (s is MyPanelStyle) { MyPanelStyle mps = (MyPanelStyle)s; if (!mps.IsEmpty) { if (mps.IsSet(PROP_BACKIMAGEURL)) this.BackImageUrl = mps.BackImageUrl; if (mps.IsSet(PROP_HORIZONTALALIGN)) this.HorizonalAlign = mps.HorizonalAlign; if (mps.IsSet(PROP_WRAP)) this.Wrap = mps.Wrap; } } } } // 重写MergeWith方法 public override void MergeWith(Style s) { if (s != null) { if (IsEmpty) { CopyFrom(s); return; } base.MergeWith(s); if (s is MyPanelStyle) { MyPanelStyle mps = (MyPanelStyle)s; if (!mps.IsEmpty) { if (mps.IsSet(PROP_BACKIMAGEURL) && !this.IsSet(PROP_BACKIMAGEURL)) this.BackImageUrl = mps.BackImageUrl; if (mps.IsSet(PROP_HORIZONTALALIGN) && !this.IsSet(PROP_HORIZONTALALIGN)) this.HorizonalAlign = mps.HorizonalAlign; if (mps.IsSet(PROP_WRAP) && !this.IsSet(PROP_WRAP)) this.Wrap = mps.Wrap; } } } } //重写Reset方法 public override void Reset() { base.Reset(); if (IsEmpty) return; if (IsSet(PROP_BACKIMAGEURL)) ViewState.Remove("BackImageUrl"); if (IsSet(PROP_HORIZONTALALIGN)) ViewState.Remove("HorizontalAlign"); if (IsSet(PROP_WRAP)) ViewState.Remove("Wrap"); } } } | 下面列举了MyPanelStyle类图。
|
|
|
|
|
|