自定义Web控件学习

  前几天在上课时,感觉很无聊,突然想起以前做过的学院官方网站的banner其实可以做成自定义控件引用到页面上的,于是翻开SDK文档看例子,学习写一下。
    文档上的例子是一个date的例子,其实就是用HtmlTextWriter 里的 Write()方法把页面元素描述出来。下面是代码(CustomControlDate.cs):
None.gif using  System;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.ComponentModel;
None.gif
None.gif
namespace  CustomConstrols
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// DateCustomControlDate 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [DefaultProperty("Message"),
InBlock.gif        ToolboxData(
"<{0}:CustomControlDate runat=server></{0}:CustomControlDate>")]
InBlock.gif    
public class CustomControlDate : Control
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string message = "您好!";
InBlock.gif
InBlock.gif        [Bindable(
true),
InBlock.gif            Category(
"Appearance"),
InBlock.gif            DefaultValue(
"")]
InBlock.gif        
public string Message
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return message;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                message 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 将此控件呈现给指定的输出参数。
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="output"> 要写出到的 HTML 编写器 </param>

InBlock.gif        protected override void Render(HtmlTextWriter writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            writer.Write(
"<font> "
InBlock.gif                
+ this.Message + "<br>" + "The time on the server is " + System.DateTime.Now.ToLongTimeString()
InBlock.gif                
+ "</font>");
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

--------------------------------------------------------------------------------

标记蓝色的代码段是定义在使用此控件时的标签写法<{0}:CustomControlDate runat=server>,在Register.aspx里是这样引用的
<?XML:NAMESPACE PREFIX = Custom /> <Custom:CustomControlDate Message="This control tells time. " runat="server" ID="CustomControlDate" />
还有一点要注意的是在Register.aspx 的头引用里要如下写,注意引用的文件。
<
%@ Page language="c#" Codebehind="Register.aspx.cs" AutoEventWireup="false" Inherits="Register.Register" % >
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Register TagPrefix="Custom" Namespace="CustomConstrols" Assembly = "CustomConstrols"  %>
ExpandedBlockStart.gifContractedBlock.gif
<% dot.gif @ Page language="c#" Codebehind="Register.aspx.cs" AutoEventWireup="false" Inherits="Register.Register"  %>
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > WebForm1 </ title >
None.gif        
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio .NET 7.1" >
None.gif        
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
None.gif        
< meta  name ="vs_defaultClientScript"  content ="JavaScript" >
None.gif        
< meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
None.gif    
</ HEAD >
None.gif    
< body  MS_POSITIONING ="GridLayout" >
None.gif        
< form  method ="post"  runat ="server" >
None.gif            Here is a custom ASP.NET server control.
None.gif            
< br >
None.gif            
< br >
None.gif            
< Custom:CustomControlDate  Message ="This control tells time. "  runat ="server"  ID ="CustomControlDate"   />
None.gif            
< FONT  face ="宋体" ></ FONT >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >


---------------------------------------------------------------------------------
(Resister.aspx.cs)
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
namespace  Register
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Register : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected CustomConstrols.CustomControlDate CustomControlDate;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

-----------------------------------------------------------------------------------
在解决方案CustomConstrols里先写好控件,然后再建Register项目,引用控件后就可以在Resgister.aspx里使用.



转载于:https://www.cnblogs.com/TV9/archive/2005/03/20/122426.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值