Access to child controls that reside in an ASP.NET user control template

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; } ... 
+6
source share
3 answers

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; } ... 
+4
source

I would make a method for accessing content controls in your user control:

 public Control FindInnerControl (string id) { if (contentHolder.Controls.Count > 0) { return contentHolder.Controls [0].FindControl (id); //The first control is your ContentContainer } return null; } 

then in the code behind your page you can do

 TextBox txtKeytbcss = (TextBox) BaseFormControl1.FindInnerControl ("keytbcss"); 
0
source

When you write a control inside a template, you give it a sample of what the content will be. Imagine that you are giving grid content in a template with some id “SomeTextBox”, SomeTextBox cannot appear several times as the lines in the grid increase. The control will call it dynamically during runtime.

But still you have a way, you need to find the container control and apply it, Control.FindControl () on it. FindControl () accepts your template control identifier that you specified during development.

0
source

Source: https://habr.com/ru/post/894045/


All Articles