【问题标题】:How to check if Itemplate property has literal text before or after calling InstantiateIn()如何在调用 InstantiateIn() 之前或之后检查 Itemplate 属性是否具有文字文本
【发布时间】:2015-01-14 17:22:38
【问题描述】:

我有一个带有 Itemplate 属性的 ASP.NET 控件 MyControl:

public Itemplate MyTemplate{ get;放; }

这允许我将 HTML 放在标记中的 MyTemplate 标记之间,这将在渲染过程中像这样被实例化:

if (MyTemplate!= null) {
    Control MyTemplateContainer = new Control();
    MyTemplate.InstantiateIn(MyTemplateContainer);
    Controls.Add(MyTemplateContainer);
}

检查MyTemplate!=null 告诉我MyTemplate 标签是否存在于标记中的控件中,但不是标签内是否有任何东西。

这个问题的解决方案,Render Control Without Page,我们调用 RenderControl 然后访问 StringWriter 将标记转换为字符串,需要首先初始化页面,我不希望这样做。有没有其他方法可以判断 MyTemplate 标签中是否有文字?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:
    if (MyTemplate != null && MyTemplate.ChildControls != null && MyTemplate.ChildControls[0] != null && MyTemplate.ChildControls[0].GetType() == typeof (Literal))
    {
      //Template has a control, and it's a literal
    }
    

    【讨论】:

    • 服务器控件内的 aspx 模板中的所有内联文本在处理页面时都被 aspx 引擎转换为 Literal 控件。
    猜你喜欢
    • 2021-11-29
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多