1
//设置引入控件初始化的设置
2
[DefaultProperty("Text"),
3
ToolboxData("<{0}:TestControl runat=server></{0}:TestControl>")]
4
public class TestControl : Control
5
{
6
7
//设置Text在VS编辑器中显示的类别与默认值
8
[Bindable(true),
9
Category("Appearance"),
10
DefaultValue("")]
11
public string Text{}
12
13
[Bindable(true), Category("Misc")]
14
public string ExMessage{}
15
16
protected override void Render(HtmlTextWriter output)
17
{
18
output.Write();
19
}
20
}
//设置引入控件初始化的设置2
[DefaultProperty("Text"),3
ToolboxData("<{0}:TestControl runat=server></{0}:TestControl>")]4
public class TestControl : Control5
{6

7
//设置Text在VS编辑器中显示的类别与默认值8
[Bindable(true),9
Category("Appearance"),10
DefaultValue("")]11
public string Text{}12

13
[Bindable(true), Category("Misc")]14
public string ExMessage{}15

16
protected override void Render(HtmlTextWriter output)17
{ 18
output.Write();19
}20
} 1
public class MyLinkControl : Control
2
{
3
4
Color color = Color.Blue;
5
string hyperLink = "http://www.wrox.com";
6
string text = "This is the Wrox site";
7
int fontSize = 20;
8
9
public Color LinkColor;
10
11
12
protected override void Render(HtmlTextWriter output)
13
{
14
output.AddAttribute(HtmlTextWriterAttribute.Href,hyperLink);
15
output.AddStyleAttribute(HtmlTextWriterStyle.FontSize,fontSize.ToString()); //添加字体的大小
16
output.AddStyleAttribute(HtmlTextWriterStyle.Color,ColorTranslator.ToHtml(color)); //添加颜色,将颜色翻译成指定HTML格式
17
output.RenderBeginTag(HtmlTextWriterTag.A); //呈现标志的开始
18
output.Write(text);
19
output.RenderEndTag(); //呈现标志的结束
20
}
21
}
public class MyLinkControl : Control2
{3
4
Color color = Color.Blue;5
string hyperLink = "http://www.wrox.com";6
string text = "This is the Wrox site";7
int fontSize = 20;8
9
public Color LinkColor;10
11
12
protected override void Render(HtmlTextWriter output)13
{14
output.AddAttribute(HtmlTextWriterAttribute.Href,hyperLink); 15
output.AddStyleAttribute(HtmlTextWriterStyle.FontSize,fontSize.ToString()); //添加字体的大小16
output.AddStyleAttribute(HtmlTextWriterStyle.Color,ColorTranslator.ToHtml(color)); //添加颜色,将颜色翻译成指定HTML格式17
output.RenderBeginTag(HtmlTextWriterTag.A); //呈现标志的开始18
output.Write(text);19
output.RenderEndTag(); //呈现标志的结束20
}21
}继承于:WebControl
1
//这个是从WebControl继承,具体的写法与从Controls继承是有区别的
2
public class MyLinkControl : WebControl
3
{
4
5
string hyperLink ;
6
string text ;
7
8
9
/*
10
11
public WebControl ( System.Web.UI.HtmlTextWriterTag tag )
12
System.Web.UI.WebControls.WebControl 的成员
13
14
摘要:
15
使用指定的 HTML 标记初始化 System.Web.UI.WebControls.WebControl 类的新实例。
16
17
参数:
18
tag: System.Web.UI.HtmlTextWriterTag 值之一。
19
*/
20
public MyLinkControl() : base (HtmlTextWriterTag.A)
21
{
22
23
}
24
25
public string Text;
26
protected override void OnInit(EventArgs e){}
27
28
public string HyperLink
29
{
30
set
31
{
32
if (value.IndexOf("http://") == -1)
33
{
34
throw new Exception("Specify Http as the protocol");
35
//在设置属性时,抛出错误提示
36
}
37
hyperLink = value;
38
}
39
}
40
41
//添加属性
42
protected override void AddAttributesToRender(HtmlTextWriter output)
43
{
44
output.AddAttribute(HtmlTextWriterAttribute.Href,hyperLink);
45
base.AddAttributesToRender(output);
46
}
47
48
//呈现内容
49
protected override void RenderContents(HtmlTextWriter output)
50
{
51
output.Write(text);
52
base.RenderContents(output);
53
}
54
}
//这个是从WebControl继承,具体的写法与从Controls继承是有区别的2
public class MyLinkControl : WebControl 3
{4
5
string hyperLink ;6
string text ;7

8

9
/*10

11
public WebControl ( System.Web.UI.HtmlTextWriterTag tag )12
System.Web.UI.WebControls.WebControl 的成员13

14
摘要:15
使用指定的 HTML 标记初始化 System.Web.UI.WebControls.WebControl 类的新实例。 16

17
参数:18
tag: System.Web.UI.HtmlTextWriterTag 值之一。19
*/20
public MyLinkControl() : base (HtmlTextWriterTag.A)21
{22

23
}24

25
public string Text;26
protected override void OnInit(EventArgs e){}27

28
public string HyperLink29
{30
set31
{32
if (value.IndexOf("http://") == -1)33
{ 34
throw new Exception("Specify Http as the protocol");35
//在设置属性时,抛出错误提示36
}37
hyperLink = value;38
}39
}40

41
//添加属性42
protected override void AddAttributesToRender(HtmlTextWriter output)43
{44
output.AddAttribute(HtmlTextWriterAttribute.Href,hyperLink);45
base.AddAttributesToRender(output);46
}47

48
//呈现内容49
protected override void RenderContents(HtmlTextWriter output)50
{51
output.Write(text);52
base.RenderContents(output);53
}54
}
如果是制作自己的表格控件的话可以public MyDataGridControl() : base (HtmlTextWriterTag.Table)
然后在
1
protected override void RenderContents(HtmlTextWriter output)
2
{
3
output.RenderBeginTag(HtmlTextWriterTag.Tr); //TR开始
4
output.RenderBeginTag(HtmlTextWriterTag.Td); //TD开始
5
output.RenderBeginTag //B开始(HtmlTextWriterTag.B);
6
output.Write("Wrox Data Display Table Control"); //输出内容
7
output.RenderEndTag();
8
output.RenderEndTag();
9
output.RenderEndTag();
10
base.RenderContents(output);
11
}
protected override void RenderContents(HtmlTextWriter output)2
{ 3
output.RenderBeginTag(HtmlTextWriterTag.Tr); //TR开始4
output.RenderBeginTag(HtmlTextWriterTag.Td); //TD开始5
output.RenderBeginTag //B开始(HtmlTextWriterTag.B);6
output.Write("Wrox Data Display Table Control"); //输出内容7
output.RenderEndTag();8
output.RenderEndTag();9
output.RenderEndTag();10
base.RenderContents(output); 11
}

浙公网安备 33010602011771号