different user control type: reuse layout vs reuse data

Reuse layout: normal User Control

Reuse data: Templated User Control
http://www.codeproject.com/Articles/397646/A-Beginners-Tutorial-for-Understanding-Templated-U

  • container :The main purpose of having this container class is to provide the FindControl functionality with the custom layout in place. The naming container-based class you’ll create inherits from Control and implements the INamingContainer interface
public class AddressUcContainer : Control, INamingContainer
{
public AddressUcContainer(Address address)
{
this.Address = address;
}
public Address Address { get; set; }
}

  • implement a property of type ITemplate,Apply the TemplateContainerAttribute attribute to the ITemplate property; this marks the property as a template.
public partial class AddressUcTemplated :
System.Web.UI.UserControl
{
protected void Page_Init(object sender, EventArgs e)
{
//clear the controls from the placeholder
PlaceHolderAddressTemplate.Controls.Clear();
if (LayoutTemplate == null)
{
PlaceHolderAddressTemplate.Controls.Add(
new LiteralControl("No template defined."));
}
else
{
AddressUcContainer container = new
AddressUcContainer(this.Address);
this.LayoutTemplate.InstantiateIn(container);
//add the controls to the placeholder
PlaceHolderAddressTemplate.Controls.Add(container);
}
}
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateContainer(typeof(AddressUcContainer))]
public ITemplate LayoutTemplate { get; set; }
public Address Address { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
//simulate getting a user and loading his or her profile
AddressUcTemplated1.Address.AddressLine1 = "1234 Some St.";
AddressUcTemplated1.Address.City = "Ann Arbor";
AddressUcTemplated1.Address.State = "MI";
AddressUcTemplated1.Address.PostalCode = "48888";
//bind data to controls
Page.DataBind();
}


  • How to use this template control
<uc1:AddressUcTemplated ID="AddressUcTemplated1"
runat="server" AddressType="Home">
<LayoutTemplate>
<h1>Edit Home Address</h1>
<table>
<tr>
<td>Address Line 1:</td>
<td>
<asp:TextBox ID="TextBoxAddress" runat="server"
Text="<%#Container.Address.AddressLine1%>"></asp:TextBox>
</td>
</tr>
<tr>
<td>Address Line 2:</td>
<td>
<asp:TextBox ID="TextBoxAddressLine2" runat="server"
Text="<%#Container.Address.AddressLine2%>"></asp:TextBox>
</td>
</tr>
<tr>
<td>City:</td>
<td>
<asp:TextBox ID="TextBoxCity" runat="server"
Text="<%#Container.Address.City%>"></asp:TextBox>
</td>
</tr>
<tr>
<td>State:</td>
<td>
<asp:TextBox ID="TextBoxState" runat="server"
Text="<%#Container.Address.State%>"></asp:TextBox>
</td>
</tr>
<tr>
<td>Postal Code:</td>
<td>
<asp:TextBox ID="TextBoxPostalCode" runat="server"
Text="<%#Container.Address.PostalCode%>"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="ButtonSave" runat="server" Text="Save"
OnClick="ButtonSave_Click"/>
</td>
</tr>
</table>
</LayoutTemplate>
</uc1:AddressUcTemplated>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值