开发者

User control that returns formated text

I am looking for a way to return some formatted text via a user control in ASP.NET C#. This is basically what I am looking for:

Call a user control such as

<mycontrol:formatedtitle id="blah" runat="server">Text to format</mycontrol:formatedtitle>

And then have it return some formatted HTML such as

<div class="blah">Text to format</div>

I have been reading up online on creating user controls but nothing comes close to what I am looking for. I was able to do this in PHP easily by calling a command and having it return the fomatted开发者_运维知识库 text such as:

<? print_section_start("Text to format"); ?>


If you want to use a UserControl, it would be fairly easy. Here's a basic outline.

Let's assume you create a user control file FormattedTitle.ascx and code behind file FormattedTitle.ascx.cs in your root folder.

Markup would be as follows:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FormattedTitle.ascx.cs"
Inherits="FormattedTitle" %>
<asp:Label ID="lblTitle" runat="server" />

The code behind for the user control would be as follows:

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);
        }
    }
}

I see the user control usage markup like so:

<%@ Register TagPrefix="uc" TagName="FormattedTitle" Src="FormattedTitle.ascx" %>
<uc:FormattedTitle ID="ftMyTitle" runat="server" Title="Title to Format" TitleFormat="SomeValidDotNetFormatString" />

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.

For full background info on string formatting in .net, consider using these resources:

  • MSDN library article

EDIT:

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:

<%@ Register TagPrefix="uc" TagName="FormattedTitle" Src="FormattedTitle.ascx" %>

<uc:FormattedTitle ID="ftMyTitle" runat="server" Title="Title to Format" TitleFormat="SomeValidDotNetFormatString" />

<uc:FormattedTitle ID="ftMyTitle2" runat="server" Title="Some Other Title to Format" TitleFormat="SomeOtherValidDotNetFormatString" />

Is this what you mean by multiple tags?


There's a number of possible things that you can do here:

  1. Create a custom control that does what you're asking for.
  2. Use <%= print_section_start("text to format") %> which is similar to what you were doing in PHP.
  3. Give the <div> tag an id and a runat="server" attribute, format the text in the code behind and then set the .InnerText property of the div


you could also give mycontrol an attribute equal to "Text to format"

<mycontrol:formatedtitle id="blah" runat="server" someAttributeName="Text to format"></mycontrol:formatedtitle>

Can you even access text in between opening and closing tags of a user control?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜