用戶自定義控件的使用(2)

除了將公共字段提升爲控件屬性外,還可以使用屬性語法。屬性語法具有能夠在設置或檢索屬性時執行代碼的優點。下面的示例說明一個 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>

 

調用面前臺:     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>";
    }

}

 

用戶自定義控件前臺:    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>&nbsp;
<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;
        }

    }


}

發佈了28 篇原創文章 · 獲贊 3 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章