# ASP.NET 自定义服务器控件的使用
最近在做一个网站后台,想要用
<xxx:xxx />
这种形式来直接读取数据库的数据,于是研究了一下自定义服务器控件。 首先,先新建一个WebCustomControl1类(WEB自定义服务器控件) VS默认生成的代码如下:
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace UniversalWebSiteBg
{
//是设置控件默认的属性的。这里是其Text属性。就是当你选择这个控件的时候,在属性窗口中自动被选中的是Text属性。
[DefaultProperty("Text")]
//这行表示,当你在工具箱拖动自定义服务器控件时,为它生成的默认标记
[ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : WebControl
{
// "Text"是这个服务器控件的一个属性
[Bindable(true)] //指定是否通常用于绑定
[Category("Appearance")] //说明该属性的分类是“外观”
[DefaultValue("")]//默认值为空
[Localizable(true)]//指定是否应本地化该属性
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
//返回输出服务器控件在网页上所显示的值。
//默认的输出结构是:<span>这里是输出的值</span>
//到这里,可能和我一样的初学者会问了,为什么默认输出的是span标记?为什么不能是a标记,为什么不能是单纯的文本?
//这里就说到了控件的渲染方法RenderContents了,VS默认是使用RenderContents方法,这样的话输出的是span标记也是固定了
//如果想输出单纯文本的话,那么就应该是使用Render,而不是RenderContents了。
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(Text);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
于是,我们将改动一下默认的渲染方法:
protected override void Render(HtmlTextWriter output)
{
output.Write(Text);
}
1
2
3
4
2
3
4
这样的话,控件输出的内容就不包含标记了。 那么,如何输出其他的标记呢?
protected override void Render(HtmlTextWriter output)
{
output.WriteBeginTag("a");//输出标记的开头
output.Write("这是一个标记");//输出标记的内容
output.WriteEndTag("a");//输出标记的结束符
}
1
2
3
4
5
6
2
3
4
5
6
这样运行的话,你会发现,html源码如下:
<a>这是一个标记</a>
1
到这里,控件的构建就完成了,但是,你要在ASPX页面中去使用它,还需要在前台页面注册它,表明它的命名空间,标记前缀等信息。 回到开头那里的
[ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
1
{0}这个就是标记前缀字段
<%@ Register Assembly="UniversalWebSiteBg" Namespace="UniversalWebSiteBg" TagPrefix="UniBg" %>
1
在这里,我写的标记前缀是 UniBg 于是,我在工具箱中拖动控件到页面中后, 会生成标记:
<UniBg:WebCustomControl1 Id="WebCustomControl1" Text="" runat="server" />
1
于是这个简单的服务器控件就完成了。