用户自定义控件的使用(2)

本文介绍如何通过属性语法增强用户控件的功能,利用TextBox控件的文本属性创建Address用户控件,实现地址信息的输入及展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

除了将公共字段提升为控件属性外,还可以使用属性语法。属性语法具有能够在设置或检索属性时执行代码的优点。下面的示例说明一个 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;
        }

    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值