扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:金属边缘 来源:天极开发 2007年11月6日
关键字: Windows
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"); } } } |
图3 |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register TagPrefix="wcl" Assembly="WebControlLibrary" Namespace="WebControlLibrary" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>类型化样式属性</title> </head> <body> <form id="form1" runat="server"> <wcl:MyPanel ID="demo1" runat="server" BackImageUrl="pic1.jpg" HorizontalAlign="Center" Height="145" Width="160"> <br /> <br /> 这是一行位于MyPanel控件中的文字。 </wcl:MyPanel> </form> </body> </html> |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者