User Control with PlaceHolder and some pre-defined elements
Enviroment: Asp.Net 3.5, VS 2008, Windows XP
Hello, i have a user control (uc:StackUC) such as:
<fieldset>
<legend>blablabla</legend>
<%-- other fields --%>
<asp:PlaceHolder ID="contentHolder" runat="server"></asp:PlaceHolder>
<asp:Button runat="server"/>
</fieldset>
My idea is to use on my pages, like this (due to lots of repetition):
...
<uc:StackUC>
<label>specific content</label>
<%-- blablabla --%>
</uc:StackUC>
...
I believe it has to do with templated user controls, however, all the examples i find have no pre-defined user controls, and are aimed at doing
<%#Container.Index %> (or any other property)
Which is not useful to me.
Thanks in advance.
References for templated user controls: http://leedale.wordpress.com/2007/08/开发者_开发百科11/creating-a-templated-user-control-with-aspnet-20/, http://msdn.microsoft.com/en-us/library/36574bf6(vs.71).aspx#Y456
The following codes are simplified.
The user-control ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BaseFormControl.ascx.cs"
Inherits="SOPR.CustomForms.BaseFormControl" %>
<fieldset class="fset1">
</fieldset>
This is my user control code-behind:
public partial class BaseFormControl : System.Web.UI.UserControl
{
[TemplateContainer(typeof(ContentContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateInstance(TemplateInstance.Single)] //IMPORTANT, makes controls visible in page code-behind
public ITemplate Content { get; set; }
void Page_Init()
{
if (Content != null)
{
ContentContainer cc = new ContentContainer();
Content.InstantiateIn(cc);
contentHolder.Controls.Add(cc);
}
}
My usage in the view:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddOperator.aspx.cs"
Inherits="SOPR.Cadastro.AddOperator" MasterPageFile="~/MasterPage.Master" %>
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" ID="maincont" runat="server" EnableViewState="true">
<uc:BaseFormControl ID="BaseFormControl1" runat="server">
<Content>
<asp:TextBox runat="server" CssClass="keytbcss" MaxLength="4" ID="keytb"
NewLine="false" />
</Content>
</uc:BaseFormControl>
This is actually a copy of the results of my other question post.
精彩评论