Luk*_*son 3 c# asp.net string placeholder
简单的问题,但我找不到任何东西,是否有可能将asp:占位符的内容添加到字符串?如果可能的话,这对服务器端来说会很棒.
卢克
如果您只想要占位符的文本内容:
string textualContent = ((LiteralControl) PlaceHolder1.Controls[0]).Text;
Run Code Online (Sandbox Code Playgroud)
返回
"Hello World"
:
<asp:PlaceHolder ID="PlaceHolder1" runat="server">Hello World</asp:PlaceHolder>
Run Code Online (Sandbox Code Playgroud)
如果你还想获得渲染控件的html(以及它的所有子控件):
System.IO.TextWriter tw = new System.IO.StringWriter();
HtmlTextWriter h = new HtmlTextWriter(tw);
PlaceHolder1.RenderControl(h);
string html = tw.ToString();
Run Code Online (Sandbox Code Playgroud)
对于这个aspx(GridView
带有一些sameple数据的数据绑定):
<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>
Run Code Online (Sandbox Code Playgroud)
这个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>
Run Code Online (Sandbox Code Playgroud)
请注意,您需要将页面指令更改为
EnableEventValidation="false"
Run Code Online (Sandbox Code Playgroud)
并覆盖VerifyRenderingInServerForm
在Page
public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
Run Code Online (Sandbox Code Playgroud)
RenderControl
手动呼叫时.
归档时间: |
|
查看次数: |
7548 次 |
最近记录: |