【问题标题】:Custom Control with Template that allows child control HTML Response to be manipulated带有模板的自定义控件,允许操作子控件 HTML 响应
【发布时间】:2015-11-30 19:56:25
【问题描述】:

我正在尝试创建一个使用 ITemplate 的 ASP.NET 自定义控件,以允许开发人员在其中放置自己的内部 ASP/HTML 控件。该控件的目的是解析模板内容的呈现 HTML,并使用特定于上下文的值搜索/替换可能出现的某些占位符文本(例如 [Location]、[Division]、[FirstName])。

我正在努力解决如何“捕获”生成的 HTML 并在响应对象之前根据需要“替换”它的各个方面。

我的想法是使用RenderContents(HtmlTextWriter output) 将输出替换为我自己的流并让控件写入该流。然后我可以在发送到output 之前对渲染的内容做我需要做的事情。

但是,我不确定这是否是正确的方法。

这是我所拥有的一个sn-p:

/// <summary>
/// This custom control allows replacement fields to be picked up
/// </summary>
[DefaultProperty("Text")]
[ToolboxData("<{0}:ReplaceableText runat=\"server\" Text=\"[Text]\"></{0}:ReplaceableText>")]
public partial class ReplaceableText : WebControl
{
    public ReplaceableText()
    {
        TokenReplacer = new TokenReplacer();
    }

    [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [TemplateInstance(TemplateInstance.Single)]
    public ITemplate Content { get; set; }

    /// <summary>
    /// The component the performs the replacement
    /// </summary>
    /// <remarks>
    /// An alternative TokenReplacer component could be installed by the caller if required.
    /// More typically, you will use the AddResolver(s) methods to apply callbacks to do the
    /// actual replacement.
    /// </remarks>
    public ITokenReplacer TokenReplacer { get; set; }



    public bool HasTokenReplacer { get { return (TokenReplacer != null && !DesignMode); } }


    protected override void RenderContents(HtmlTextWriter output)
    {
        System.Diagnostics.Debug.WriteLine("RenderContents() enter");

        if (HasTokenReplacer)
        {
            using (var memStream = new MemoryStream())
            {
                using (var sw = new StreamWriter(memStream))
                {
                    using (var htw = new HtmlTextWriter(sw))
                    {
                        base.RenderContents(htw);
                    }
                }
                // !! memStream is still empty (0 bytes) !!

                var text = Encoding.UTF8.GetString(memStream.GetBuffer());
                var text2 = TokenReplacer.Convert(text);
                output.Write(text2);
            }
        }
        else
        {
            base.RenderContents(output);
        }

        output.Write(ConvertedText);
        System.Diagnostics.Debug.WriteLine("RenderContents() exit");
    }


    protected override void CreateChildControls()
    {
        if (Content != null)
        {
            Controls.Clear();
            Content.InstantiateIn(this);    // !!! I need this to write to my own alternative temporary stream 
        }
        base.CreateChildControls();
    }

很遗憾,我的记忆流中似乎没有任何标记。

【问题讨论】:

    标签: asp.net custom-controls


    【解决方案1】:

    我是个傻瓜,我没有在我的ContentsITemplate 中添加任何控件。 这个解决方案似乎运作良好,只要确保您检查 Controls.Count &gt; 0 如果您发现您的 MemoryStream 以 0 字节结尾。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多