如上代码所示,主要设置了@ Register指令和自定义服务器控件Company。前者用于为页面引入自定义服务器控件Company,从而实现控件在页面中的应用。在自定义服务器控件Company中主要设置了City、Employee-Name、Employee-Sex和Employee-Title。同时,在开发人员编码过程中将会发现,以上4个属性均为Visual Studio 2005的智能感知功能所支持。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLibrary{
[DefaultProperty("Text")]
[ToolboxData("<{0}:Company runat=server></{0}:Company>")]
public class Company : WebControl {
private Employee employee; //实现属性City
[ Bindable(true), Category("Appearance"), DefaultValue(""), Description("公司所在城市") ]
public string City {
get {
string _city = (String)ViewState["City"];
return ((_city == null)?String.Empty:_city);
}
set { ViewState["City"] = value; }
} //实现属性Employee