如何用代码中的控件替换占位符?

时间:2015-12-25 15:31:10

标签: c# asp.net

我希望我的用户能够使用##CreateLink##这样的占位符创建模板,这些占位符应该由控件代替。

我知道如何在运行时创建控件但我不知道如何用这些动态创建的控件替换占位符。

我怎样才能做到这一点?

这是我的.aspx代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Literal ID="lit" runat="server" Text="This is my sample text. Here is my ##token## which should be replaced by my control in codebehind..."></asp:Literal>
    </div>
    </form>
</body>
</html>

这是我的代码隐藏:

   public partial class WebForm1 : System.Web.UI.Page
    {
        private HyperLink lnk;

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

            lnk = new HyperLink();
            lnk.ID = "lnk";
            lnk.NavigateUrl = "http://www.google.com";
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
        }
    }

2 个答案:

答案 0 :(得分:1)

使用渲染方法

protected override void Render(HtmlTextWriter writer)
{
        //Place replace logic

}

<强>更新

protected override void Render(HtmlTextWriter writer)
{
    MemoryStream stream = new MemoryStream();
    StreamWriter memWriter = new StreamWriter(stream);
    HtmlTextWriter myWriter = new HtmlTextWriter(memWriter);

    base.Render(myWriter);
    myWriter.Flush();

    stream.Position = 0;

    string renderedHtml = new StreamReader(stream).ReadToEnd();

    renderedHtml = renderedHtml.Replace("##token##", "VALUE");

    writer.Write(renderedHtml);
    writer.Close();
    myWriter.Close();
    stream.Close();
}

答案 1 :(得分:1)

使用another answer指导我,我制定了以下内容:

.aspx Markup

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Literal ID="lit" runat="server" Text="This is my sample text. Here is my ##token## and my button ##button## which should be replaced by my control in codebehind..."></asp:Literal>
        <p>this is a default button</p>
        <asp:Button ID="btn2" runat="server" text="from asp.net" />
    </div>
    </form>
</body>
</html>

C#CodeBehind代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private HyperLink lnk;
        private Button btn;

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

            lnk = new HyperLink();
            lnk.ID = "lnk";
            lnk.Text = "<b>Sample Link</b>";
            lnk.NavigateUrl = "http://www.google.com";

            btn = new Button();
            btn.ID = "btn";
            btn.Text = "button text";
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void Render(HtmlTextWriter writer)
        {
            StringBuilder sb = new StringBuilder();
            HtmlTextWriter myWriter = new HtmlTextWriter(new System.IO.StringWriter(sb, System.Globalization.CultureInfo.InvariantCulture));

            base.Render(myWriter);

            myWriter.Flush();

            string renderedHtml = sb.ToString();
            renderedHtml = renderedHtml.Replace("##token##", RenderHTML(lnk));
            renderedHtml = renderedHtml.Replace("##button##", RenderHTML(btn));

            writer.Write(renderedHtml);
            myWriter.Close();
            sb.Clear();
        }

        private string RenderHTML(Control ctrl)
        {
            StringBuilder sb = new StringBuilder();  
            HtmlTextWriter myWriter = new HtmlTextWriter(new System.IO.StringWriter(sb, System.Globalization.CultureInfo.InvariantCulture));

            ctrl.RenderControl(myWriter);

            myWriter.Close();
            return sb.ToString();
        }
    }
}