User control(用户控件) vs custom control(自定义控件)
2008-03-26 11:25
776 查看
通俗点说,用户控件就是你可以在visual studio工具栏里面拖一个或多个控件,然后像编写普通aspx页面那样就可以开发,所以开发难度相对较低。而定制控件没有这种可视化的支持,需要在自 己的头脑中展现这个控件的可视化输出,通过改写RenderContents(HtmlTextWriter output)方法来定义。下面一个非常简单的Hello world的例子分别用两种控件来展示:在页面显示“hello world”
首先用user control:
1. 在visual studio 2005中新建一个站点:
2. Add new item: web user control:
3. 双击WebUserControl.ascx,切换到Design视图,从工具栏里拖一个label过来,把Text属性改为:Hello world.
4. 将WebUserControl拖到Default.aspx中, visual studio 会自动注册这个用户控件。 Default.aspx代码如下:
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%
@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
</div>
</form>
</body>
</html>
5. 按F5运行网站:
现在用定制控件:
1.新建一个项目web control library, 并把这个项目加入到网站solution中。
2. 双击WebCustomControl1.cs,可以看见存在一个Text属性,所以在这个hello world例子里我们不需要增加任何代码。编译并产生一个dll.
3. 把这个dll文件拷贝到网站的Bin目录下, 并修改Default.aspx,如下:
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%
@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<%
@ Register TagPrefix="cc1" Namespace="WebControlLibrary1" Assembly="WebControlLibrary1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
<cc1:WebCustomControl1 Text="Hello world from custom control" runat="server" />
</div>
</form>
</body>
</html>
4. 按F5运行:
注:在页面注册控件也可以通过web.config来完成,这样可以不用再每个页面文件头上写<%@ Register ...%>语句。
首先用user control:
1. 在visual studio 2005中新建一个站点:
2. Add new item: web user control:
3. 双击WebUserControl.ascx,切换到Design视图,从工具栏里拖一个label过来,把Text属性改为:Hello world.
4. 将WebUserControl拖到Default.aspx中, visual studio 会自动注册这个用户控件。 Default.aspx代码如下:
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%
@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
</div>
</form>
</body>
</html>
5. 按F5运行网站:
现在用定制控件:
1.新建一个项目web control library, 并把这个项目加入到网站solution中。
2. 双击WebCustomControl1.cs,可以看见存在一个Text属性,所以在这个hello world例子里我们不需要增加任何代码。编译并产生一个dll.
3. 把这个dll文件拷贝到网站的Bin目录下, 并修改Default.aspx,如下:
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%
@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<%
@ Register TagPrefix="cc1" Namespace="WebControlLibrary1" Assembly="WebControlLibrary1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
<cc1:WebCustomControl1 Text="Hello world from custom control" runat="server" />
</div>
</form>
</body>
</html>
4. 按F5运行:
注:在页面注册控件也可以通过web.config来完成,这样可以不用再每个页面文件头上写<%@ Register ...%>语句。
相关文章推荐
- WPF中用户控件对比自定义控件(UserControl VS CustomControl)
- WPF之路——用户控件对比自定义控件(UserControl VS CustomControl)
- WPF之路——用户控件对比自定义控件UserControl VS CustomControl)
- WPF之路——用户控件对比自定义控件UserControl VS CustomControl)
- WPF之路——用户控件对比自定义控件UserControl VS CustomControl)
- 用户控件(usercontrol)与自定义控件的区别(CustomControl)
- 自定义控件(usercontrol)与用户控件(customcontrol)区别??
- 自定义控件中UserControl(用户控件)和CustomControl(自定义控件)的区别
- 用户控件(usercontrol)与自定义控件的区别(CustomControl)
- 用户控件 vs 自定义控件
- VS中Component Class、User Control及Custom Control的区别 .
- 自定义控件VS用户控件
- C# 自定义控件VS用户控件
- 用户控件 vs 自定义控件
- C#自定义控件VS用户控件
- 如何让Web自定义控件(WebCustomControl)能够被 验证控件 验证
- C# 自定义控件VS用户控件
- 自定义控件VS用户控件
- 自定义控件的使用以及与用户控件的区别
- 将ASP.NET用户控件转化为自定义控件