<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Namespace="WebControlLibrary" Assembly="WebControlLibrary" TagPrefix="Cp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
<title>实现连字符形式复杂属性</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<Cp:Company ID="demo1" runat="server" City="重庆" Employee-Name="小李" Employee-Sex="男" Employee-Title="销售经理" />
</div>
</form>
</body>
</html>
<Cp:Company ID="Company1" runat="server" City="重庆">
<Employee Name="小李" Sex="男" Title="销售经理">
</Employee>
</Cp:Company>
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
[ Bindable(true), Category("Appearance"), Description("员工信息"), DesignerSerializationVisibility( DesignerSerializationVisibility.Content), NotifyParentProperty(true) ]
public Employee Employee {
get {
if (employee == null) {
employee = new Employee();
}
return employee;
}
} // 重写RenderContents方法,自定义实现控件呈现
protected override void RenderContents(HtmlTextWriter output) {
output.Write("公司所在城市:");
output.Write(City);
output.WriteBreak();
output.Write("姓名:");
output.Write(Employee.Name.ToString());
output.WriteBreak();
output.Write("性别:");
output.Write(Employee.Sex.ToString());
output.WriteBreak();
output.Write("职务:");
output.Write(Employee.Title.ToString());
}
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Web.UI;
namespace WebControlLibrary{
public class Employee {
private string _name;
private string _sex;
private string _title; //实现构造函数1
public Employee() { } //实现构造函数2
public Employee(String Name, String Sex, String Title) {
_name = Name; _sex = Sex; _title = Title;
} //实现属性Name
[ Bindable(true), Category("Appearance"), DefaultValue(""), Description("员工姓名"), NotifyParentProperty(true) ]
public String Name {
get { return _name; }
set { _name = value; }
} //实现属性Sex
[ Bindable(true), Category("Appearance"), DefaultValue(""), Description("员工性别"), NotifyParentProperty(true) ]
public String Sex {
get { return _sex; }
set { _sex = value; }
} //实现属性Title
[ Bindable(true), Category("Appearance"), DefaultValue(""), Description("员工职务"), NotifyParentProperty(true) ]
public String Title {
get { return _title; }
set { _title = value; }
}
}
}