Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to use a <code>UserControl</code>, it would be fairly easy. Here's a basic outline.</p> <p>Let's assume you create a user control file <code>FormattedTitle.ascx</code> and code behind file <code>FormattedTitle.ascx.cs</code> in your root folder. </p> <p><strong>Markup would be as follows:</strong></p> <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeFile="FormattedTitle.ascx.cs" Inherits="FormattedTitle" %&gt; &lt;asp:Label ID="lblTitle" runat="server" /&gt; </code></pre> <p><strong>The code behind for the user control would be as follows:</strong></p> <pre><code>using System; using System.Collections.Generic; using System.Runtime.Serialization; using System.Web; using System.Web.UI; public partial class FormattedTitle : System.Web.UI.UserControl { public string Title { get { return this.lblTitle.Text; } set { this.lblTitle.Text = value; } } public string TitleFormat { get { if(ViewState["TitleFormat"] != null) return ViewState["TitleFormat"].ToString(); return string.Empty; } set { ViewState["TitleFormat"] = value; } } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if(!string.IsNullOrEmpty(this.TitleFormat)) { this.lblTitle.Text = string.Format(this.lblTitle.Text, this.TitleFormat); } } } </code></pre> <p>I see the user control usage markup like so:</p> <pre><code>&lt;%@ Register TagPrefix="uc" TagName="FormattedTitle" Src="FormattedTitle.ascx" %&gt; &lt;uc:FormattedTitle ID="ftMyTitle" runat="server" Title="Title to Format" TitleFormat="SomeValidDotNetFormatString" /&gt; </code></pre> <p>Not tested, but should get you started. With the above user control, you can set the title and its formatting as properties of the user control.</p> <p>For full background info on string formatting in .net, consider using these resources:</p> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/system.string.format.aspx" rel="nofollow noreferrer">MSDN library article</a> </li> </ul> <p><strong>EDIT</strong>:</p> <p>To answer your question re: multiple tags - do you mean multiple instances of the control on a page? This is actually the nature and purpose of user controls - you can add them as often as needed, such as:</p> <pre><code>&lt;%@ Register TagPrefix="uc" TagName="FormattedTitle" Src="FormattedTitle.ascx" %&gt; &lt;uc:FormattedTitle ID="ftMyTitle" runat="server" Title="Title to Format" TitleFormat="SomeValidDotNetFormatString" /&gt; &lt;uc:FormattedTitle ID="ftMyTitle2" runat="server" Title="Some Other Title to Format" TitleFormat="SomeOtherValidDotNetFormatString" /&gt; </code></pre> <p>Is this what you mean by multiple tags?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload