ASP:占位内容字符串 [英] asp:placeholder contents to string
问题描述
简单的问题,但我无法找到任何东西,是有可能得到一个asp的内容:占位符为字符串?这将是巨大的做服务器端,如果它是可能的。
Simple question but I can't find anything on it, is it possible to get the contents of an asp:placeholder to a string? This would be great to do server side if it is possible.
路
推荐答案
如果你只是想要一个占位符的文本内容:
If you just want the textual content of a placeholder:
string textualContent = ((LiteralControl) PlaceHolder1.Controls[0]).Text;
返回
为:
你好,世界
<asp:PlaceHolder ID="PlaceHolder1" runat="server">Hello World</asp:PlaceHolder>
如果你也想获得控制渲染的HTML(和它所有的儿童控件):
If you also want to get the html of the rendered control (and all of it's child-controls):
System.IO.TextWriter tw = new System.IO.StringWriter();
HtmlTextWriter h = new HtmlTextWriter(tw);
PlaceHolder1.RenderControl(h);
string html = tw.ToString();
有关这个ASPX(即 GridView控件
数据绑定是与一些sameple数据):
For this aspx (the GridView
is databound with some sameple data):
<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>
这个网站会生成(依赖于浏览器):
this html will be generated (depends on the browser):
<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>
请注意,你需要改变你的页面指令
Note that you need to change your page directive to
EnableEventValidation="false"
和重写 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
手动
这篇关于ASP:占位内容字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!