The following codes are simplified.
Ascx user control:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BaseFormControl.ascx.cs" Inherits="SOPR.CustomForms.BaseFormControl" %> <fieldset class="fset1"> </fieldset>
This is my user management code:
public partial class BaseFormControl : System.Web.UI.UserControl { [TemplateContainer(typeof(ContentContainer))] [PersistenceMode(PersistenceMode.InnerProperty)] public ITemplate Content { get; set; } void Page_Init() { if (Content != null) { ContentContainer cc = new ContentContainer(); Content.InstantiateIn(cc); contentHolder.Controls.Add(cc); } }
My use in 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>
I'm trying to access the "keytb" control for code, but it doesn't exist like this (for example, using a variable that doesn't exist). Any ideas?
Thanks in advance.
DECISION --------------------------
I found a pretty nice solution, just add [TemplateInstance (TemplateInstance.Single)] to the ITemplate property of the user control and everything will be visible. Now I can use this as a regular page control.
public partial class BaseFormControl : System.Web.UI.UserControl { [TemplateContainer(typeof(ContentContainer))] [PersistenceMode(PersistenceMode.InnerProperty)] [TemplateInstance(TemplateInstance.Single)] public ITemplate Content { get; set; } ...
source share