using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Drawing; namespace XiuYanger.ProCSharp.Concrols ...{ [DefaultProperty("Text")] publicclass SimpleDiv1 : Control ...{ //Text property [ Category("Appearance"), DefaultValue(""), Description("The text in the div lable."), ] publicstring Text ...{ get ...{ object o = ViewState["Text"]; return (o ==null) ?string.Empty : (string)o; } set ...{ ViewState["Text"] = value; } } //FontColor Property [ Category("Appearance"), DefaultValue(typeof(Color), "Black"), Description("The text in the div lable."), TypeConverter(typeof(WebColorConverter)) ] public Color FontColor ...{ get ...{ object o = ViewState["FontColor"]; return (o ==null) ? Color.Black : (Color)o; } set ...{ ViewState["FontColor"] = value; } } //override RenderMothed protectedoverridevoid Render(HtmlTextWriter writer) ...{ writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID); writer.AddStyleAttribute(HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(FontColor)); writer.RenderBeginTag(HtmlTextWriterTag.Div); writer.Write(this.Text); writer.RenderEndTag(); } } }
继承自WebControl类的:
using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Drawing; namespace XiuYanger.ProCSharp.Concrols ...{ [DefaultProperty("Text")] publicclass SimpleDiv:WebControl ...{ //Text property [ Category("Appearance"), DefaultValue(""), Description("The text in the div lable."), ] publicstring Text ...{ get ...{ object o=ViewState["Text"]; return (o ==null) ?string.Empty : (string)o; } set ...{ ViewState["Text"]=value; } } //override KeyTag property protectedoverride HtmlTextWriterTag KeyTag ...{ get ...{ return HtmlTextWriterTag.Div; } } //override RenderContentMethod protectedoverridevoid RenderContents(HtmlTextWriter writer) ...{ writer.Write(this.Text); } } }