【问题标题】:asp:placeholder contents to stringasp:占位符内容到字符串
【发布时间】:2012-12-28 14:51:27
【问题描述】:

简单的问题,但我在上面找不到任何东西,是否可以将 asp:placeholder 的内容获取到字符串?如果可能的话,这将是很棒的服务器端。

卢克

【问题讨论】:

  • 什么是 "asp:placeholder 到字符串的内容"?你想要生成的 html 还是想要服务器端占位符的文本内容?

标签: c# asp.net string placeholder


【解决方案1】:

试试PlaceholderRenderControl方法。

我为HyperLink 做了类似的事情,但是作为Placeholder 继承自System.Web.UI.Control,它的工作方式应该完全相同。像这样的:

StringBuilder sb = new System.Text.StringBuilder();
using (var stringWriter = new System.IO.StringWriter(sb))
{
    using (var htmlTextWriter = new System.Web.UI.HtmlTextWriter(stringWriter))
    {
        YourPlaceHolder.RenderControl(htmlTextWriter);
    }
}
return sb.ToString();

我写了一篇关于这个话题的短文:http://www.tomot.de/en-us/article/3/asp.net/create-a-control-in-the-codebehind-and-retrieve-its-rendered-output

【讨论】:

    【解决方案2】:

    如果您只想要占位符的文本内容:

    string textualContent = ((LiteralControl) PlaceHolder1.Controls[0]).Text;
    

    为:返回 "Hello World"

    <asp:PlaceHolder ID="PlaceHolder1" runat="server">Hello World</asp:PlaceHolder>
    

    如果您还想获取渲染控件(以及所有子控件)的 html:

    System.IO.TextWriter tw = new System.IO.StringWriter();
    HtmlTextWriter h = new HtmlTextWriter(tw);
    PlaceHolder1.RenderControl(h);
    string html = tw.ToString();
    

    对于这个 aspx(GridView 与一些相同的数据进行数据绑定):

    <asp:PlaceHolder ID="PlaceHolder1" runat="server">
        <asp:Label ID="LblTest" runat="server">Test-Label</asp:Label>
        <asp:TextBox ID="TxtTest" runat="server" Text="Foo"></asp:TextBox>
        <asp:GridView runat="server" ID="GridView1"></asp:GridView>
        <textarea name="TextArea1" rows="2" cols="1">
        First line
        Second line
        </textarea>
    </asp:PlaceHolder>
    

    将生成此 html(取决于浏览器):

    <span id="MainContent_LblTest">Test-Label</span><input name="ctl00$MainContent$TxtTest" type="text" value="Foo" id="MainContent_TxtTest" /><div>
        <table cellspacing="0" rules="all" border="1" id="MainContent_GridView1" style="border-collapse:collapse;">
            <tr>
                <th scope="col">ID</th><th scope="col">Text</th>
            </tr><tr>
                <td>1</td><td>Row #1</td>
            </tr><tr>
                <td>2</td><td>Row #2</td>
            </tr><tr>
                <td>3</td><td>Row #3</td>
            </tr><tr>
                <td>4</td><td>Row #4</td>
            </tr><tr>
                <td>5</td><td>Row #5</td>
            </tr>
        </table>
    </div>
        <textarea name="TextArea1" rows="2" cols="1">
        First line
        Second line
        </textarea>
    

    请注意,您需要将页面指令更改为

    EnableEventValidation="false"
    

    并在Page 中覆盖VerifyRenderingInServerForm

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }
    

    手动调用RenderControl时。

    【讨论】:

    • 我是否需要在 VerifyRenderingInServerForm 的覆盖中添加任何内容才能使其正常工作?目前我的中继器控件不起作用?
    • 抱歉,这不是一个很有帮助的描述。中继器中没有呈现任何内容,我在被调用的用户控件的页面加载中放置了一个断点并且它没有被命中。 web.config 中是否有针对 EnableEventValidation="false" 的全局设置? ascx 中的所有代码都可以工作并显示,但 c# 代码隐藏没有被调用?
    猜你喜欢
    • 1970-01-01
    • 2014-08-12
    • 2015-09-17
    • 2013-07-31
    • 2016-10-23
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 2017-10-20
    相关资源
    最近更新 更多