3

I have an asp.net UserControl that writes HTML content from a SharePoint list to a Literal control. Currently I insert a title in h5 tags before each SharePoint list item I write to the Literal. Instead of having the title hardcoded to be placed in h5 tags, I'd like to expose a public property of my user control that lets me define the html format for the title. This is a little different from the templated user control questions I've found so many of because it's not really a template for the user control. I just need a string containing html. Here's what I'm looking to do:

public class MyUserControl: UserControl
{
    public string TitleFormat { get; set; }

    private void ShowContent()
    {
        ...
        string output = String.Format(TitleFormat, title) + someContent;
        ltlOutput.Text = output.
    }
}

In markup:

<UC:MyUserControl id="muc1" runat="server">
    <TitleFormat>
        <a href="www.somewhere.com"><h3>{0}</h3></a>
    </TitleFormat>
</UC:MyUserControl>

How can I set this up?

2 Answers 2

2

Here is the answer (provided by Decker Dong in the asp.net forums):

To nest another class into one, you have to declare a new property but just declare it is an InnerProperty. And set its Design properties. Now here's a full sample for you:

[ParseChildren(true),PersistChildren(false)]
public partial class MyUserControl : System.Web.UI.UserControl
{
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public string TitleFormat
    {
        get;
        set;
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

With these attributes you can use the control as written in the question.

Sign up to request clarification or add additional context in comments.

Comments

-1

What you need is http://msdn.microsoft.com/en-us/library/system.web.ui.itemplate.aspx

HTML

<test:NamingControl runat="server" ID="NamingControl" TitleFormat="This is myTitle">
    <TitleFormatTemplate>
         My title is <%# Container.TitleFormat %>
   </TitleFormatTemplate>
</test:NamingControl>

UserControl

public partial class MyUserControl : System.Web.UI.UserControl
{
    private ITemplate template;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string TitleFormat
    {
        get;
        set;
    }

    [PersistenceMode(PersistenceMode.InnerProperty), 
    TemplateContainer(typeof(TitleFormatTemplate))]
    public ITemplate TitleFormatTemplate
    {
        get { return template; }
        set { template = value; }
    }

    protected override void CreateChildControls()
    {
        base.CreateChildControls();

        TitleFormatTemplate t = new TitleFormatTemplate();
        t.TitleFormat = this.TitleFormat;
        template.InstantiateIn(t);
        this.Controls.Add(t);
        this.DataBind();
    }
}

Child Control - INamingContainer

public class TitleFormatTemplate : Control, INamingContainer
{
    private string _TitleFormat = "";
    public string TitleFormat
    {
        get { return _TitleFormat; }
        set { _TitleFormat = value; }
    }
}

The simpler approach - No more TitleFormat tag

MyUserControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs"      
    Inherits="testgingweb.usrcontrols.MyUserControl" %>

<a href="www.somewhere.com"><h3><asp:Label runat="server" ID="PassedValueLabel"></asp:Label</h3></a>

The Codebehind - MyUserControl.ascx.cs

public string TitleFormat
{
    get { return ViewState["TitleFormat"]; }
    set { ViewState["TitleFormat"] = value; }
}

protected void Page_Load(object sender, EventArgs e)
{
    PassedValueLabel.Text = String.Format("Whatever {0} here", this.TitleFormat);
}

HTML

<test:MyUserContorl runat="server" ID="NamingControl" TitleFormat="This is myTitle">
</test:MyUserContorl>

Notice that I didn't have the TitleFormat tag anymore.

3 Comments

First, thanks for your answer. I don't think this will work for what I'm doing. You've sort of redefined my goal. I'm going to be inserting a dynamic number of titles so I can't have a property for each one. I don't really need an ITemplate or anything instantiated in the control either. It would be overkill. I just want to declare a string property as a child of the user control in markup. The value of the string property just happens to be HTML. Are you saying that can't be done?
The markup <TitleFormat> you used as child element to your control can only work with ITemplate. Without that you can't do it declaratively. Check my updated answer to see the other approach
"The markup <TitleFormat> you used as child element to your control can only work with ITemplate" - False. See marked answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.