VS.NET 2005中的Web控件一种是通过添加添加新项中的“添加Web用户控件”,在设计器里设计用户控件界面,在web页面通过<%@ Register Src="Controls/AddressForm.ascx" TagName="AddressForm" TagPrefix="PetShopControl" %>来注册。
一、使用App_Code,不通过编译,直接使用。
1:新建一个网站,添加WelcomeControl.cs,这时VS会自动添加一个文件夹App_Code,并将该.cs文件添加
到目录下。
2:在文件中添加如下代码
1
2 using System.Data;
3 using System;
4 using System.ComponentModel;
5 using System.Security.Permissions;
6 using System.Web;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9
10 namespace PetShopCopy.Controls
11 {
12 [AspNetHostingPermission(System.Security.Permissions.SecurityAction.Demand,
13 Level = AspNetHostingPermissionLevel.Minimal),
14 AspNetHostingPermission(System.Security.Permissions.SecurityAction.InheritanceDemand,
15 Level = AspNetHostingPermissionLevel.Minimal),
16 DefaultProperty( " Text " ),
17 ToolboxData( " <{0}:WelComeControl runat=\ " server\ " ></{0}:WelComeControl> " )
18 ]
19 public class WelComeControl : WebControl
20 {
21 [
22 Bindable( true ),
23 Category( " Appearance " ),
24 DefaultValue( " 匿名 " ),
25 Description( " 欢迎的标题 " ),
26 Localizable( true )
27 ]
28 public virtual string Text
29 {
30 get
31 {
32 string s = ( string )ViewState[ " Text " ];
33 return (s == null ) ? String.Empty : s;
34
35 }
36 set
37 {
38 ViewState[ " Text " ] = value;
39
40 }
41 }
42 protected override void RenderContents(HtmlTextWriter writer)
43 {
44 writer.WriteEncodedText(Text);
45 if (Context != null )
46 {
47 string s = Context.User.Identity.Name;
48 if (s != null && s != string .Empty)
49 {
50 string [] split = s.Split( ' \\ ' );
51 int n = split.Length - 1 ;
52 if (split[n] != String.Empty)
53 {
54 writer.Write( " , " );
55 writer.Write(split[n]);
56 }
57 }
58 writer.Write( " ! " );
59 writer.Write( " 这个是测试控件哦,好漂亮! " );
60 }
61 // base.RenderContents(writer);
62 }
63
64 }
65 }
3:添加default.aspx页面
2 using System.Data;
3 using System;
4 using System.ComponentModel;
5 using System.Security.Permissions;
6 using System.Web;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9
10 namespace PetShopCopy.Controls
11 {
12 [AspNetHostingPermission(System.Security.Permissions.SecurityAction.Demand,
13 Level = AspNetHostingPermissionLevel.Minimal),
14 AspNetHostingPermission(System.Security.Permissions.SecurityAction.InheritanceDemand,
15 Level = AspNetHostingPermissionLevel.Minimal),
16 DefaultProperty( " Text " ),
17 ToolboxData( " <{0}:WelComeControl runat=\ " server\ " ></{0}:WelComeControl> " )
18 ]
19 public class WelComeControl : WebControl
20 {
21 [
22 Bindable( true ),
23 Category( " Appearance " ),
24 DefaultValue( " 匿名 " ),
25 Description( " 欢迎的标题 " ),
26 Localizable( true )
27 ]
28 public virtual string Text
29 {
30 get
31 {
32 string s = ( string )ViewState[ " Text " ];
33 return (s == null ) ? String.Empty : s;
34
35 }
36 set
37 {
38 ViewState[ " Text " ] = value;
39
40 }
41 }
42 protected override void RenderContents(HtmlTextWriter writer)
43 {
44 writer.WriteEncodedText(Text);
45 if (Context != null )
46 {
47 string s = Context.User.Identity.Name;
48 if (s != null && s != string .Empty)
49 {
50 string [] split = s.Split( ' \\ ' );
51 int n = split.Length - 1 ;
52 if (split[n] != String.Empty)
53 {
54 writer.Write( " , " );
55 writer.Write(split[n]);
56 }
57 }
58 writer.Write( " ! " );
59 writer.Write( " 这个是测试控件哦,好漂亮! " );
60 }
61 // base.RenderContents(writer);
62 }
63
64 }
65 }
4:注册控件:有两种方式注册:1)在页面顶端<%@ Register Namespace="PetShopCopy.Controls"
TagPrefix="aspSample" %> 2)在web.config <System.web>节添加
1
<
system
.web
>
2 < pages >
3 < controls >
4 < add tagPrefix ="aspSample" namespace ="PetShopCopy.Controls" />
5 </ controls >
6 </ pages >
5:添加aspsSample控件
2 < pages >
3 < controls >
4 < add tagPrefix ="aspSample" namespace ="PetShopCopy.Controls" />
5 </ controls >
6 </ pages >
<
aspSample:WelComeControl
BackColor
=wheat
ForeColor
=saddleBrown
id
="WelComeControl1"
runat
="server"
>
</ aspSample:WelComeControl >
</ aspSample:WelComeControl >
二、编译成程序集
编译成程序集有利于分发给其他软件小组成员使用你编写的控件。并且可以在工具箱中显示,如果
winform中的控件那样。
1:添加环境变量
我的电脑->属性->高级->环境变量/系统变量/ 打开path属性编辑,在后面加分号“;”,
然后是.net Framework的安装路径通常是:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
2:在App_Code目录添加AssemblyInfo.cs的文件,文件内容
using
System;
using System.Web.UI;
[assembly:TagPrefix( " PetShopCopy.Controls " , " aspSample " )]
目的是当从工具箱中将控件拖到页面时,自动在页面顶端生成注册代码。它建立了命名空间和前缀
using System.Web.UI;
[assembly:TagPrefix( " PetShopCopy.Controls " , " aspSample " )]
之间额映射
3:编译控件成程序集
在VS.NET2005命令提示下,将路径定位到App_Code目录下
执行命令:
“csc /res:sample.bmp,PetShopCopy.Controls.WelcomeControl.bmp
/t:library /out:PetShopCopy.Controls.dll /r:System.dll /r:System.Web.dll WelcomeControl.cs”
其中/res是定义控件显示的图标。/t:表示创建一个程序集 /out程序集名称/r连接到程序集的程序集 后面是.cs源代码。
4:web.config中的注册
1
<
system.web
>
2 < pages >
3 < controls >
4 < add tagPrefix = " aspSample " namespace = " PetShopCopy.Controls " assembly = " PetShopCopy.Controls " />
5 </ controls >
6 </ pages >
assembly就是程序集的名称。
2 < pages >
3 < controls >
4 < add tagPrefix = " aspSample " namespace = " PetShopCopy.Controls " assembly = " PetShopCopy.Controls " />
5 </ controls >
6 </ pages >
5:最后一步就是开始使用控件了,在工具箱中添加新项,然后将其拖放到web页面