Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6759239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:54:44+00:00 2026-05-26T13:54:44+00:00

I am searching for this all around the Internet, including here, for 2 days

  • 0

I am searching for this all around the Internet, including here, for 2 days now, but I am not able to find anything similar being dealt with. Also, this is my first post on Stack Overflow, so bear with me please :]

The problem:

This is my first time actually creating a templated server control, but I accomplished this without problems with the help of MSDN on the templating topics. I have implemented a control that can be used like this:

<test:TemplatedControl ID="templatedControl" runat="server">
    <Template>
        Template start:
        <%# Container.EmailText %>
        <%# Container.EmailField %>
        Template end.
    </Template>
</test:TemplatedControl>

The control uses the following container/context for the template:

public class TemplateContainer : WebControl, INamingContainer
{
    public string EmailText
    {
        get
        {
            return this.emailLabelText;
        }
    }
    string emailLabelText;


    public TextBox EmailField
    {
        get
        {
            return this.emailField;
        }
    }
    TextBox emailField;


    public TemplateContainer()
        : base(HtmlTextWriterTag.Div)
    {
        this.BorderWidth = 2;
        this.BorderStyle = BorderStyle.Outset;

        this.emailLabelText = "E-mail:";

        this.emailField = new TextBox();
        this.emailField.MaxLength = 10;
    }
}

Both properties of the container class work ok and are accessible via data binding expressions while writing the template, but EmailField is being rendered as System.Web.UI.WebControls.TextBox, which is ok by the given code. The output of the control is:

<div style="border-width:2px;border-style:Outset;">
    Template start:
    E-mail:
    System.Web.UI.WebControls.TextBox
    Template end.
</div>

Now the question is: Is it possible to output a control (such as TextBox here) as actual control back into the template as other data via binding expressions?

If this would be possible, then it would be possible to pick apart the output of this control including its child controls and put it together as needed via the template.

I am afraid that this may not be possible to achieve at all, but I would like to get this clarified by someone. If that’s the case, I am also open to suggestions of another approach. If you are interested in why I am trying to do this, read below.

The reason for this approach:

I am building a composite server control, which customer himself will use on his site, which is still in development. Therefore the final layout and formatting needs for the output of the control are not known yet. Instead the control is required to have flexible output in terms of both layout and design, so it could be customized to match the site later.

The design part of this can be realized by setting some CSS class properties to the control, though it may be a mess to configure for the customer, as the output of the control will be quite complex.

I am not sure about how to achieve the customizable layout requirement e.g., render the form fields inside table structure, or render it in divs, or maybe just inline without additional HTML, etc.

So instead of providing several pre made types of output, I came up with an idea, that the output rendered by my control could be templated, which could solve both layout and design flexibility problem. Customer would declaratively set a template to wrap around the stuff that my template needs to render, which would also enable him to set all his CSS classes right there to wrapping elements as needed. And this leads me to aforementioned problem and question.

Thanks in advance,

Garkin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:54:44+00:00Added an answer on May 26, 2026 at 1:54 pm

    I think you need an abstract class that defines what the TemplateContainer is required to have. For Example:

    public abstract class TemplateContainer : WebControl, INamingContainer
    {
        public abstract Literal EmailText { get; }
        public abstract TextBox EmailField { get; }
    }
    

    And then require the test:TemplateControl Template to implement TemplateContainer.

    This way you can use EmailText and EmailField to apply MaxLength etc. Although you probably want to verify that they are not null before using them

    if(EmailText != null)
    {
        //use EmailText
    }
    

    The consumer of the server control would then be creating a control UserTemplate.ascx similar to this:

    Template start:
    <asp:Literal ID="emailText" runat="server" />
    <asp:TextBox ID="emailField" runat="server" />
    Template end.
    

    With a code behind UserTemplate.ascx.cs:

    public class UserTemplate : TemplateContainer
    {
        public Literal EmailText { get { return emailText; } }
        public TextBox EmailField { get { return emailField; } }
    }
    

    —-Edit—-

    Based on all of this your page that contains test:TemplatedControl will look like (assuming the path to UserTemplate.ascx is aliased as test:UserTemplate):

    <test:TemplatedControl ID="templatedControl" runat="server">
        <Template>
            <test:UserTemplate ID="userTemplate" runat="server" />
        </Template>
    </test:TemplatedControl>
    

    And then in the code behind you can access the properties on UserTemplate:

    templatedControl.Template.EmailText.Text = "Email: ";
    templatedControl.Template.EmailField.MaxLength = 10;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been searching all around but still cannot find a solution for this problem. My
Been searching all over for this but cant find an answer so thought I
I've tried searching around but couldn't find a solution to this. I am running
I have been searching around for answers, but I can't seem to find anything.
I've been searching all up and down google trying to find this as well
First of all, I did some searching for an answer to this question...but I
Ok, I have tried searching around for this answer, but with no luck. I
This has probably been asked to death around here, but I could never get
I've been searching around but I just can't seem to find what I'm looking
I think this question is probably fairly simple, but I've been searching around and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.