阅读ASP.Net控件的原始ItemTemplate标记

时间:2012-08-12 01:31:49

标签: asp.net

我想知道是否可以从转发器控件中读取ItemTemplate的原始标记?

考虑下面的转发器:

<asp:Repeater ID="uiReport" runat="server" EnableViewState="False">
    <HeaderTemplate>
        <table border="1">
            <thead>
                <tr>
                    <th>Product</th>
                </tr>
            </thead>
            <tbody>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><%#((Product)Container.DataItem).ProductName%></td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </tbody></table>
    </FooterTemplate>
</asp:Repeater> 

我正在尝试以原始字符串格式读取项目模板,例如字符串,如下所示:

string itemTemplate = "<tr><td><%#((Product)Container.DataItem).ProductName%></td></tr>"

使用Reflector并查看调用堆栈,可以看到下面的调用堆栈屏幕抓取方法(见第一行突出显示):

call stack

我假设从 ...__ BuildControl__control6()... 中.aspx页面中标记的原始内容被读取,切断(RegEx?)并作为参数传递以下方法:

System.Web.UI.DataBoundLiteralControl.SetStaticString(index, s);

对于此示例中的Repeater控件,'s'(字符串)参数的值为:

\r\n                <tr>\r\n                    <td>

请注意,字符串已在&lt;%#。

的第一个实例上拆分

MSDN搜索确认SetStaticString方法支持.NET Framework基础结构,不能直接在您的代码中使用。

http://msdn.microsoft.com/en-us/library/system.web.ui.databoundliteralcontrol.setstaticstring.aspx

是否存在我缺少的虚拟方法,它允许我获取原始模板标记,或者是我自己读取文件内容的唯一选项(下面的代码示例)并覆盖必要的 Render()< / em> Control基类的方法?

将手动读取.aspx页面内容的代码的详细示例:

string rawPageMarkUp = File.ReadAllText(physicalPathOfAspxPage);
string rawItemTemplate = RegExMethodToExtractItemTemplateFromControl(controlName, rawPageMarkUp);

1 个答案:

答案 0 :(得分:0)

答案似乎是否定,没有可用于获取未解析页面的虚拟方法。处理解析页面/控件的方法由密封类和内部方法组成。

此问题的解决方案是问题中描述的替代建议。

下面是类的详细信息,以便您可以查看本机实现,以了解代码的外观。

从以下密封班开始:

System.Web.UI.PageParser

这继承自抽象类:

System.Web.UI.TemplateControlParser

该类继承了一个名为:

的方法
ParseFile

按照继承的课程进行操作:

System.Web.UI.TemplateParser

您将在此处找到包含读取原始文件和解析内容的起点的方法:

internal void ParseFile(string physicalPath, VirtualPath virtualPath)
{
    string o = (physicalPath != null) ? physicalPath : virtualPath.VirtualPathString;
    if (this._circularReferenceChecker.Contains(o))
    {
        this.ProcessError(SR.GetString("Circular_include"));
    }
    else
    {
        this._circularReferenceChecker.Add(o);
        try
        {
            StreamReader reader;
            if (physicalPath != null)
            {
                using (reader = Util.ReaderFromFile(physicalPath, base.CurrentVirtualPath))
                {
                    this.ParseReader(reader, virtualPath);
                    return;
                }
            }
            using (Stream stream = virtualPath.OpenFile())
            {
                reader = Util.ReaderFromStream(stream, base.CurrentVirtualPath);
                this.ParseReader(reader, virtualPath);
            }
        }
        finally
        {
            this._circularReferenceChecker.Remove(o);
        }
    }
}