如何将asp控件渲染为HTML?

时间:2014-10-14 11:39:01

标签: asp.net vb.net

我的应用程序我需要一个asp页面的html版本所以我开始使用

将asp转换为html

在代码段

下面
Public Function GetInnerHtml()
  Dim allTextBoxValues As String = ""
  Dim c As Control
  Dim childc As Control
  For Each c In Page.Controls
    For Each childc In c.Controls
      allTextBoxValues &= RenderControlToHtml(childc)
    Next
  Next
  Return allTextBoxValues
End Function

Public Function RenderControlToHtml(ByVal ControlToRender As Control) As String
  Try
    Dim sb As New System.Text.StringBuilder()
    Dim stWriter As New System.IO.StringWriter(sb)
    Dim htmlWriter As New System.Web.UI.HtmlTextWriter(stWriter)
    ControlToRender.RenderControl(htmlWriter)
    Return sb.ToString()

  Catch ex As Exception

  End Try
End Function

但是通过上面的代码我只能获得标签的html,但是我无法为这样的前端代码生成html

<div id="Drug" runat="server" > <div>
<asp:RadioButton ID="RdBtnQ2No" runat="server"  GroupName="Question2" Text="No" 
  Font-Size="12px" CssClass="scaledRadioButton" Height="20px" Width="49px"/>

我的意思是在服务器上运行的控件正在抛出异常

说:

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

但我已将所有控件放在<form runat="server">标记中。

1 个答案:

答案 0 :(得分:0)

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="plainHtml.aspx.vb" EnableEventValidation="false" Inherits="Default3" %>

我在.aspx页面中添加了“EnableEventValidation =”false“它现在运行良好

相关问题