Custom Controls and User Controls

VB.NET用户控件用于C#页面
文章指出VB.NET的用户控件可用于C#页面,不仅页眉页脚是常见用户控件,任何.aspx页面都可转换为用户控件。作者写此文章是为梳理备考30 - 315考试的概念,希望能帮助他人。
Custom Controls and User Controls both have sense of "customized" control. There is a bit of confusing, isn't it?  Well, myself have developed a couple of user controls for my projects. I did quite frequently referring them as Custom
Controls
..which is wrong. Those are actual User Controls.

Custom Controls are deriving from either System.Web.UI.Control or System.Web.UI.WebControls.WebControl. (Webcontrols have additional UI properites). They are customized by overriding the Control's Render method with HtmlTextWriter. User Controls are basically .ascx files.

A simple Custom Control:
None.gifusing System; 
None.gif
using System.Web.UI; 
None.gif
using System.Web.UI.WebControls; 
None.gif
None.gif
namespace MyCustomControl 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif     
public class MyCustomControl : Control 
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif
InBlock.gif            
protected override void Render( HtmlTextWriter writer) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif             writer.Write(
"My New Custom Control!"); 
ExpandedSubBlockEnd.gif            }
 
ExpandedSubBlockEnd.gif     }
 
ExpandedBlockEnd.gif}

None.gif

To use this custom control in your page,  you need to register it with Register directive before you can use it.

None.gif<%@ Register TagPrefix="MyControl" Namespace="MyCustomControl" Assembly="MyCustomControl" %> 
None.gif
<html> 
None.gif
<body> 
None.gif     
<form runat="server"> 
None.gif        Most introductory applications output: 
None.gif        
<MyControl:MyCustomControl runat="server" /> 
None.gif     
</form> 
None.gif
</body> 
None.gif
</html>
None.gif

User Controls are quite frequently some header and footer files.  e.g. footer.ascx
None.gif<%@ Control Language="VB" %>
None.gif
<p align="left">
None.gif    
<font size="1">© 2004, AAA Inc.
None.gif    
<br />

None.gif    Comments and suggestions, please forward to ME
</font> 
None.gif
</p>

None.gif

You can drag the footer.ascx directly into your page, while Visual Studio will take care of the registration and implementation. e.g.
None.gif<%@ Page language="c#" %> 
None.gif
<%@ Register TagPrefix="uc0" TagName="Footer" Src="Footer.ascx" %>
None.gif
<html> 
None.gif
<body> 
None.gif     
<form runat="server"> 
None.gif      
<uc0:Footer id="UserControl1" runat="server"></uc0:Footer> 
None.gif     
</form> 
None.gif
</body> 
None.gif
</html>

It worths to metion that, a User Control with VB.NET can be used  in C# pages.

Not only header and footer are common User Controls, but any .aspx page can be converted to a User Control by changing

None.gif<%@ Page Language="VB" %>

into

None.gif<%@ Control Language="VB" %>

Just write this article to clear some concepts in my mind on the road to prepare 30-315 exam. Hope helps someone.

转载于:https://www.cnblogs.com/netspring/archive/2005/08/06/208718.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值