Jeremy
Jeremy

Reputation: 46350

UserControl with definable content (How to created templated user control)

A panel web control lets a developer put the panel on a page and define content that will appear inside the panel.

Is it possible to achieve similar functionality with a user control, where I define all my custom 'chrome' in the control, but am able to drop it on a page wherever I want and define the content inside on a per instance basis?

Upvotes: 1

Views: 126

Answers (4)

Jeremy
Jeremy

Reputation: 46350

As Hanlet Escaño pointed out, this is complete possible to do. I'll show:

This is the user control markup:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TemplatedUserControl.ascx.cs" Inherits="Templated_User_Control.TemplatedUserControl" %>
<table border="1">
    <tr>
        <th>Head</th>
    </tr>
    <tr>
        <td>
            <asp:PlaceHolder ID="plContent" runat="server"></asp:PlaceHolder>
        </td>
    </tr>
    <tr>
        <td>Foot</td>
    </tr>
</table>

This is the user control code:

using System;
using System.Web.UI;

namespace Templated_User_Control
{
    public partial class TemplatedUserControl : System.Web.UI.UserControl
    {
        private ITemplate content_m = null;

        [TemplateContainer(typeof(ContentContainer))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ITemplate Content
        {
            get
            {
                return content_m;
            }
            set
            {
                content_m = value;
            }
        }

        void Page_Init()
        {
            if (content_m != null)
            {
                ContentContainer container = new ContentContainer();

                //This is the real magic.  Take the contents, and put it in our ContentContainer object, then
                //add the ContentContainer object as a child control of the placeholder so it renders inside the user control
                content_m.InstantiateIn(container);
                this.plContent.Controls.Add(container);
            }
        }

        protected void Page_Load(object sender, EventArgs e) {}
        public class ContentContainer : Control, INamingContainer {}
    }
}

This is the page markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Templated_User_Control.Default" %>
<%@ Register src="TemplatedUserControl.ascx" tagname="TemplatedUserControl" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc1:TemplatedUserControl ID="TemplatedUserControl1" runat="server">
            <Content>
                Template user control test.<br />
                <asp:Literal ID="litTest" runat="server">this is a test!</asp:Literal><br />
                abc 123!
            </Content>
        </uc1:TemplatedUserControl>
    </div>
    </form>
</body>
</html>

Upvotes: 1

IrishChieftain
IrishChieftain

Reputation: 15253

Options:

1) Templated Controls as referred to by Uwe and Hanlet

2) jQuery UI library

3) ASP.NET 2.0 Web Parts

It's really tricky to implement no matter which way you go.

Upvotes: 1

Ray K
Ray K

Reputation: 1490

Yes, this is completely possible.

Here is a great example where a developer overrides an existing control (I chose an article with a Panel, since you used it as an example) and still allows content within.

The article is good, but you may find the sample code even more helpful.

Upvotes: 0

Hanlet Esca&#241;o
Hanlet Esca&#241;o

Reputation: 17380

I think you are talking about a Templated User Control. Good luck!

Upvotes: 2

Related Questions