Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing child controls that are in a templated user control ASP.NET

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)]
        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>

I'm trying to access the "keytb" control on the code-behind, but its like it didnt exist (like using a variable that doesn't exist). Any ideas?

Thanks in advance.

SOLUTION --------------------------
I found a quite nice solution, just add [TemplateInstance(TemplateInstance.Single)] to the ITemplate property of the user control and everything gets seen. I can now use just like it was a normal page control.

public partial class BaseFormControl : System.Web.UI.UserControl
{



[TemplateContainer(typeof(ContentContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateInstance(TemplateInstance.Single)]
public ITemplate Content { get; set; }
...
like image 575
WoF_Angel Avatar asked Aug 01 '11 17:08

WoF_Angel


1 Answers

SOLUTION --------------------------
I found a quite nice solution, just add [TemplateInstance(TemplateInstance.Single)] to the ITemplate property of the user control and everything gets seen. I can now use just like it was a normal page control.

public partial class BaseFormControl : System.Web.UI.UserControl
{



[TemplateContainer(typeof(ContentContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateInstance(TemplateInstance.Single)]
public ITemplate Content { get; set; }
...
like image 77
WoF_Angel Avatar answered Nov 14 '22 23:11

WoF_Angel