最近正在拜读Dr.Shahram Khosravi的《Professional ASP.NET 2.0 Server Control And Component Development》(英文版),有一种如沐春风的感觉,因此本人准备将学习过程中的体会一一记下。
今天晚上看了关于创建Custom control的部分内容,初步了解了创建Custom control的过程,特记录如下:
一、 新建一个类库(如:类名为CreditCardForm, namespace为CustomComponents),该类继承自System.Web.UI.Control。建议包含如下引用:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
二、 重写(override)该类的Render函数,并将要实现特定功能的HTML代码写在writer.Write()方法中,例如要实现以下界面:
参考代码为:
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<table style='width:287px;height:124px;border-width:0;'>");
writer.Write("<tr>");
writer.Write("<td>Payment Method</td>");
writer.Write("<td>");
writer.Write("<select name='PaymentMethod' id='PaymentMethod' style='width:100%;'>");
writer.Write("<option value='0'>Visa</option>");
writer.Write("<option value='1'>MasterCard</option>");
writer.Write("</select>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td>Credit Card No.</td>");
writer.Write("<td><input name='CreditCardNo' id='CreditCardNo' type='text' /></td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td>Cardholder's Name</td>");
writer.Write("<td><input name='CardholderName' id='CardholderName' type='text' /></td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td>Expiration Date</td>");
writer.Write("<td>");
writer.Write("<select name='Month' id='Month'>");
for (int day = 1; day < 13; day++)
{
if (day < 10)
writer.Write("<option value='" + day.ToString() + "'>" + "0" + day.ToString() + "</option>");
else
writer.Write("<option value='" + day.ToString() + "'>" + day.ToString() + "</option>");
}
writer.Write("</select>");
writer.Write(" ");
writer.Write("<select name='Year' id='Year'>");
for (int year = 2005; year < 2015; year++)
{
writer.Write("<option value='" + year.ToString() + "'>" +
year.ToString() + "</option>");
}
writer.Write("</select>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td align='center' colspan='2'>");
writer.Write("<input type='submit' value='Submit' />");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("</table>");
base.Render(writer);
}
三、 添加程序集信息文件AssemblyInfo.cs,在最后添加[assembly: AssemblyKeyFile("KeyFile.snk")],此时无法编译生成dll文件,因为必须先获取KeyFile.snk文件(具体功能我目前还不清楚),生成KeyFile.snk文件的方法为:
1、 打开Microsoft Visual Studio 2005->Visual Studio Tools-> Visual Studio 2005命令提示
2、 在命令提示符中进入程序所在目录(即.cs文件所在目录)
3、 输入sn -k KeyFile.snk
此时可成功编译生成dll文件(如:CustomComponents)
四、 使用Custom control。有两种方法可以在ASP.NET中使用Custom control,其前提都是在程序中添加引用(网站->添加引用)
方法一:直接在.cs文件创建实例并显示。首先声明该Custom control,然后在CreateChildControls()中实例化,最后在RenderControl(HtmlTextWriter writer)中绘制。参考代码如下:
public partial class _Default : System.Web.UI.Page
{
private CreditCardForm ccf;
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void CreateChildControls()
{
ccf = new CreditCardForm();
base.CreateChildControls();
}
public override void RenderControl(HtmlTextWriter writer)
{
ccf.RenderControl(writer);
base.RenderControl(writer);
}
}
方法二:在aspx文件中添加:
<%@ Register Assembly="CustomComponents" Namespace="CustomComponents" TagPrefix="custom" %>
<custom:CreditCardForm ID="CreditCardForm1" runat="server">
</custom:CreditCardForm>
TagPrefix表示的是前缀,随便你改,完整代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Assembly="CustomComponents" Namespace="CustomComponents" TagPrefix="custom" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<custom:CreditCardForm ID="CreditCardForm1" runat="server">
</custom:CreditCardForm>
</div>
</form>
</body>
</html>
2008-03-04 22:59