除了将公共字段提升为控件属性外,还可以使用属性语法。属性语法具有能够在设置或检索属性时执行代码的优点。下面的示例说明一个 Address 用户控件,该控件在内部包装了 TextBox 控件的文本属性。这样做的优点是控件可以无偿继承 TextBox 控件的自动状态管理功能。
注意,在包含 Web 窗体的页上有两个 Address 用户控件,它们分别将 Caption 属性设置为“Billing Address”和“Shipping Address”。用户控件的真正威力在于这种可重用性。
调用面前台: Default.aspx
<%
...
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
%>
<% ... @ Register Src="myuc.ascx" TagName="myuc" TagPrefix="uc1" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 无标题页 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< uc1:myuc id ="ShipAddr" runat ="server" Caption ="发货地址" Address ="浙江省宁波市" City ="宁波" State ="浙江" Zip ="315000" >
</ uc1:myuc >
< br />
< br />
< uc1:myuc id ="BillAddr" runat ="server" Caption ="收货地址" >
</ uc1:myuc >< br />
< br />
< asp:Button ID ="Button1" runat ="server" OnClick ="Button1_Click" Text ="确定" />< br />
< br />
< asp:Label ID ="Label1" runat ="server" Text ="Label" ></ asp:Label >< br />
< br />
</ div >
</ form >
</ body >
</ html >
<% ... @ Register Src="myuc.ascx" TagName="myuc" TagPrefix="uc1" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 无标题页 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< uc1:myuc id ="ShipAddr" runat ="server" Caption ="发货地址" Address ="浙江省宁波市" City ="宁波" State ="浙江" Zip ="315000" >
</ uc1:myuc >
< br />
< br />
< uc1:myuc id ="BillAddr" runat ="server" Caption ="收货地址" >
</ uc1:myuc >< br />
< br />
< asp:Button ID ="Button1" runat ="server" OnClick ="Button1_Click" Text ="确定" />< br />
< br />
< asp:Label ID ="Label1" runat ="server" Text ="Label" ></ asp:Label >< br />
< br />
</ div >
</ form >
</ body >
</ html >
调用面前台: Default.aspx.cs
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;
public partial class _Default : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
//if(!Page.IsPostBack)
//{
Label1.Text = "";
//}
}
protected void Button1_Click(object sender, EventArgs e)
...{
Label1.Text += "<b>发货地址:</b>"
+ ShipAddr.Address + ","
+ ShipAddr.City + ","
+ ShipAddr.State + ","
+ ShipAddr.Zip + "<p>";
Label1.Text += "<b>帐单地址:</b>"
+ BillAddr.Address+","
+ BillAddr.City + ","
+ BillAddr.State + ","
+ BillAddr.Zip + "<p>";
}
}
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;
public partial class _Default : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
//if(!Page.IsPostBack)
//{
Label1.Text = "";
//}
}
protected void Button1_Click(object sender, EventArgs e)
...{
Label1.Text += "<b>发货地址:</b>"
+ ShipAddr.Address + ","
+ ShipAddr.City + ","
+ ShipAddr.State + ","
+ ShipAddr.Zip + "<p>";
Label1.Text += "<b>帐单地址:</b>"
+ BillAddr.Address+","
+ BillAddr.City + ","
+ BillAddr.State + ","
+ BillAddr.Zip + "<p>";
}
}
用户自定义控件前台: myuc.ascx
<%
...
@ Control Language="C#" AutoEventWireup="true" CodeFile="myuc.ascx.cs" Inherits="myuc"
%>
< asp:Label ID ="Label1" runat ="server" Text ="<%=Caption %>" ></ asp:Label >
< br />
< asp:Label ID ="Label5" runat ="server" Text ="地址:" ></ asp:Label >
< asp:TextBox ID ="txtAddress" runat ="server" Width ="465px" ></ asp:TextBox >< br />
< asp:Label ID ="Label2" runat ="server" Text ="城市:" ></ asp:Label >
< asp:TextBox ID ="txtCity" runat ="server" Width ="104px" ></ asp:TextBox >
< asp:Label ID ="Label3" runat ="server" Text ="州:" ></ asp:Label >
< asp:TextBox ID ="txtState" runat ="server" Width ="87px" ></ asp:TextBox >
< asp:Label ID ="Label4" runat ="server" Text ="邮政编码:" ></ asp:Label >
< asp:TextBox ID ="txtZip" runat ="server" Width ="142px" ></ asp:TextBox >
< asp:Label ID ="Label1" runat ="server" Text ="<%=Caption %>" ></ asp:Label >
< br />
< asp:Label ID ="Label5" runat ="server" Text ="地址:" ></ asp:Label >
< asp:TextBox ID ="txtAddress" runat ="server" Width ="465px" ></ asp:TextBox >< br />
< asp:Label ID ="Label2" runat ="server" Text ="城市:" ></ asp:Label >
< asp:TextBox ID ="txtCity" runat ="server" Width ="104px" ></ asp:TextBox >
< asp:Label ID ="Label3" runat ="server" Text ="州:" ></ asp:Label >
< asp:TextBox ID ="txtState" runat ="server" Width ="87px" ></ asp:TextBox >
< asp:Label ID ="Label4" runat ="server" Text ="邮政编码:" ></ asp:Label >
< asp:TextBox ID ="txtZip" runat ="server" Width ="142px" ></ asp:TextBox >
用户自定义控件前台: myuc.ascx.cs
using
System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class myuc : System.Web.UI.UserControl
... {
protected void Page_Load(object sender, EventArgs e)
...{
}
//定义属性
public string Caption="地址";
public string Address
...{
get...{
return txtAddress.Text;
}
set...{
txtAddress.Text = value;
}
}
public string City
...{
get ...{
return txtCity.Text;
}
set...{
txtCity.Text = value;
}
}
public string State
...{
get ...{
return txtState.Text;
}
set ...{
txtState.Text = value;
}
}
public string Zip
...{
get ...{
return txtZip.Text;
}
set ...{
txtZip.Text = value;
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class myuc : System.Web.UI.UserControl
... {
protected void Page_Load(object sender, EventArgs e)
...{
}
//定义属性
public string Caption="地址";
public string Address
...{
get...{
return txtAddress.Text;
}
set...{
txtAddress.Text = value;
}
}
public string City
...{
get ...{
return txtCity.Text;
}
set...{
txtCity.Text = value;
}
}
public string State
...{
get ...{
return txtState.Text;
}
set ...{
txtState.Text = value;
}
}
public string Zip
...{
get ...{
return txtZip.Text;
}
set ...{
txtZip.Text = value;
}
}
}