using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
namespace WebControlsByLyj
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:HyperLink runat=server></{0}:HyperLink>")]
public class HyperLink : System.Web.UI.WebControls.WebControl
{
[Description("要显示的链接名")]
[DefaultValue("Socan信息港")]
[Category("Appearance")]
[Bindable(true)]
public string Text
{
get
{
string s = (string)ViewState["Text"];
return (s == null) ? "Socan信息港" : s;
}
set
{
ViewState["Text"] = value;
}
}
[Description("要链接到的地址")]
[DefaultValue("http://www.socan.com.cn")]
[Category("Data")]
[Bindable(true)]
public string Link
{
get
{
string s = (string)ViewState["Link"];
return (s == null) ? "http://www.socan.com.cn" : s;
}
set
{
ViewState["Link"] = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
[Description("控件摆放的位置")]
[Category("Appearance")]
public LocationInfo Location
{
get
{
LocationInfo l = (LocationInfo)ViewState["Location"];
return l == null ? new LocationInfo(0, 0) : l;
}
set
{
ViewState["Location"] = value;
}
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
//这是好的方式
writer.AddAttribute("class", CssClass);
writer.AddAttribute("href", Link);
writer.AddAttribute("target", "_blank");
//writer.AddStyleAttribute("color", "red");
writer.RenderBeginTag("a");
writer.Write(Text);
writer.RenderEndTag();
//这是不好的方式
//writer.Write("<a href='" + Link + "' style='color:red'>" + Text + "</a>");
}
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public class LocationInfo
{
private int _x;
[NotifyParentProperty(true)]
public int X
{
get { return _x; }
set { _x = value; }
}
private int _y;
[NotifyParentProperty(true)]
public int Y
{
get { return _y; }
set { _y = value; }
}
public LocationInfo(int x, int y)
{
_x = x;
_y = y;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
namespace WebControlsByLyj
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:HyperLink runat=server></{0}:HyperLink>")]
public class HyperLink : System.Web.UI.WebControls.WebControl
{
[Description("要显示的链接名")]
[DefaultValue("Socan信息港")]
[Category("Appearance")]
[Bindable(true)]
public string Text
{
get
{
string s = (string)ViewState["Text"];
return (s == null) ? "Socan信息港" : s;
}
set
{
ViewState["Text"] = value;
}
}
[Description("要链接到的地址")]
[DefaultValue("http://www.socan.com.cn")]
[Category("Data")]
[Bindable(true)]
public string Link
{
get
{
string s = (string)ViewState["Link"];
return (s == null) ? "http://www.socan.com.cn" : s;
}
set
{
ViewState["Link"] = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
[Description("控件摆放的位置")]
[Category("Appearance")]
public LocationInfo Location
{
get
{
LocationInfo l = (LocationInfo)ViewState["Location"];
return l == null ? new LocationInfo(0, 0) : l;
}
set
{
ViewState["Location"] = value;
}
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
//这是好的方式
writer.AddAttribute("class", CssClass);
writer.AddAttribute("href", Link);
writer.AddAttribute("target", "_blank");
//writer.AddStyleAttribute("color", "red");
writer.RenderBeginTag("a");
writer.Write(Text);
writer.RenderEndTag();
//这是不好的方式
//writer.Write("<a href='" + Link + "' style='color:red'>" + Text + "</a>");
}
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public class LocationInfo
{
private int _x;
[NotifyParentProperty(true)]
public int X
{
get { return _x; }
set { _x = value; }
}
private int _y;
[NotifyParentProperty(true)]
public int Y
{
get { return _y; }
set { _y = value; }
}
public LocationInfo(int x, int y)
{
_x = x;
_y = y;
}
}
}
比起上一个示例所增加的部分都加粗了,如果以前设计过Winform的控件,比较容易理解,其它的就不讲了,主要看一下属性的描述:
DesignerSerializationVisibility:指定属性是否以及如何在代码中序列化
NotifyParentProperty(true):指示当此特征应用到的属性的值被修改时是否通知其父属性
TypeConverter(typeof(ExpandableObjectConverter)):告诉属性浏览器提供扩展和折叠样式