获取占位符内的所有控件并转换为HTML [英] Get all controls inside placeholder and convert to HTML

查看:87
本文介绍了获取占位符内的所有控件并转换为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hye all ..



在我的aspx页面中,我已声明

Hye all..

In my aspx page, I''ve declared

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







从代码背后,我添加了控件(例如:复选框,网格视图,面板到占位符。

我知道每次页面回发时,所有动态控件都会消失。我发现有两种方法可以避免这些控件消失。

1)重新创建所有动态控件并添加到placheholder。

2)渲染占位符内的所有控件并转换为字符串为HTML。



我更喜欢方法#2,方法#1需要更长的时间才能执行。



对于选择的方法,我已点击按钮完成此操作:








From code behind, I added controls (eg:checkboxes, gridviews,panels) into placeholder.
I know each time page is postback, all dynamic controls will disappear. I found that there are 2 ways to avoid those controls disappearing.
1) Recreate all dynamic controls and add into placheholder.
2) Render all controls inside placeholder and convert to string as HTML.

I prefer method #2 as method #1 will takes longer time to execute.

For chosen method, I''ve done this on a button click:


Dim cb as New Checkbox
cb.ID = "cb_745"

PlaceHolder1.Controls.Add(cb)
Dim html As String = RenderControl(PlaceHolder1)

< br $> b $ b






Private Function RenderControl(control As Control) As String

       Dim sb As New StringBuilder()
       Dim sw As New StringWriter(sb)
       Dim writer As New HtmlTextWriter(sw)

       control.RenderControl(writer)
       Return sb.ToString()
   End Function







运行代码时,我得到的错误是




When run the code, error that I get is

Control ''MainContent_cb_745'' of type ''CheckBox'' must be placed inside a form tag with runat=server.





我正在使用主页。

Any帮助将不胜感激。谢谢。



I am using masterpage.
Any help would be appreciated. Thank you.

推荐答案

当我在那篇文章中找到与上述相同的错误时,我正在搜索Print aspx页面。我刚添加了这段代码:



I was searching about Print aspx page when I found in that article about error same as mine stated above. I just added this piece of code :

Public Overrides Sub VerifyRenderingInServerForm(control As Control)
        ' Verifies that the control is rendered

    End Sub









它成功执行了!动态控件不需要重新创建,但只能使用HTML并从HTML页面中的函数中检索。





And it successfully executed! The dynamic control doesnt need to be recreated, but only using HTML and retrieve from function inside HTML page.


这篇关于获取占位符内的所有控件并转换为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆