自定义Web控件学习
前几天在上课时,感觉很无聊,突然想起以前做过的学院官方网站的banner其实可以做成自定义控件引用到页面上的,于是翻开SDK文档看例子,学习写一下。
文档上的例子是一个date的例子,其实就是用HtmlTextWriter 里的 Write()方法把页面元素描述出来。下面是代码(CustomControlDate.cs):
--------------------------------------------------------------------------------
标记蓝色的代码段是定义在使用此控件时的标签写法<{0}:CustomControlDate runat=server>,在Register.aspx里是这样引用的
<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" %>
---------------------------------------------------------------------------------
(Resister.aspx.cs)
-----------------------------------------------------------------------------------
在解决方案CustomConstrols里先写好控件,然后再建Register项目,引用控件后就可以在Resgister.aspx里使用.
文档上的例子是一个date的例子,其实就是用HtmlTextWriter 里的 Write()方法把页面元素描述出来。下面是代码(CustomControlDate.cs):
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace CustomConstrols
{
/// <summary>
/// DateCustomControlDate 的摘要说明。
/// </summary>
[DefaultProperty("Message"),
ToolboxData("<{0}:CustomControlDate runat=server></{0}:CustomControlDate>")]
public class CustomControlDate : Control
{
private string message = "您好!";
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Message
{
get
{
return message;
}
set
{
message = value;
}
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<font> "
+ this.Message + "<br>" + "The time on the server is " + System.DateTime.Now.ToLongTimeString()
+ "</font>");
}
}
}
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace CustomConstrols
{
/// <summary>
/// DateCustomControlDate 的摘要说明。
/// </summary>
[DefaultProperty("Message"),
ToolboxData("<{0}:CustomControlDate runat=server></{0}:CustomControlDate>")]
public class CustomControlDate : Control
{
private string message = "您好!";
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Message
{
get
{
return message;
}
set
{
message = value;
}
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<font> "
+ this.Message + "<br>" + "The time on the server is " + System.DateTime.Now.ToLongTimeString()
+ "</font>");
}
}
}
--------------------------------------------------------------------------------
标记蓝色的代码段是定义在使用此控件时的标签写法<{0}:CustomControlDate runat=server>,在Register.aspx里是这样引用的
还有一点要注意的是在Register.aspx 的头引用里要如下写,注意引用的文件。
<%@ Page language="c#" Codebehind="Register.aspx.cs" AutoEventWireup="false" Inherits="Register.Register" %>
<%@ Register TagPrefix="Custom" Namespace="CustomConstrols" Assembly = "CustomConstrols" %>
<%@ Page language="c#" Codebehind="Register.aspx.cs" AutoEventWireup="false" Inherits="Register.Register" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form method="post" runat="server">
Here is a custom ASP.NET server control.
<br>
<br>
<Custom:CustomControlDate Message="This control tells time. " runat="server" ID="CustomControlDate" />
<FONT face="宋体"></FONT>
</form>
</body>
</HTML>
<%@ Page language="c#" Codebehind="Register.aspx.cs" AutoEventWireup="false" Inherits="Register.Register" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form method="post" runat="server">
Here is a custom ASP.NET server control.
<br>
<br>
<Custom:CustomControlDate Message="This control tells time. " runat="server" ID="CustomControlDate" />
<FONT face="宋体"></FONT>
</form>
</body>
</HTML>
---------------------------------------------------------------------------------
(Resister.aspx.cs)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Register
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class Register : System.Web.UI.Page
{
protected CustomConstrols.CustomControlDate CustomControlDate;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
Web 窗体设计器生成的代码
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Register
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class Register : System.Web.UI.Page
{
protected CustomConstrols.CustomControlDate CustomControlDate;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
Web 窗体设计器生成的代码
}
}
-----------------------------------------------------------------------------------
在解决方案CustomConstrols里先写好控件,然后再建Register项目,引用控件后就可以在Resgister.aspx里使用.