将当前网页保存为字符串,其中包含标签中的数据

时间:2014-10-07 10:18:53

标签: c# asp.net .net vb.net

我需要澄清天气,我可以将网页保存到 stringbuilder ,还是将按钮点击的字符串保存为HTML

我的HTML页面是

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form2" runat="server">
   <div onload="disableBackButton();">


    <table align="center" width="100%" cellpadding="0" cellspacing="0">

    <tr>
    <td>


        <table align="center" width="100%" cellpadding="0" cellspacing="0">
        <tr>
                <td> 

                        <div style="padding: 5px; cursor: pointer; vertical-align: middle;">
                            <div style="float: left; font-size:large; color:Blue; width: 358px;">
                             (  Heading)&nbsp;</div>
                            <div style="float: left; margin-left: 20px;">
                                </div>
                            <div style="float: right; vertical-align: middle;">
                             </div>
                        </div>

                </td>
            </tr>
            <tr>
                <td>


                        <table cellpadding="5">
                            <tr >
                                <td align="left" valign="top" style="height: 18px">
                                    <asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="#003399" Font-Size="14pt"
                                        Font-Names="Times New Roman" Text="Name"></asp:Label>
                                </td>
                                <td style="width: 175px">
                                    <asp:TextBox ID="TextBox1" runat="server" Width="170px" Height="20px"></asp:TextBox>

                                </td>
                                <%--<td width="320px">--%>
                                <td width="250px">


                                </td>
                                <td>
                                <asp:Label ID="Label2" runat="server" Font-Bold="True" ForeColor="#003399" Font-Size="14pt"
                                        Font-Names="Times New Roman" Text="Date"></asp:Label>
                                </td>
                                <td>
                                <asp:TextBox ID="txtDate" runat="server" Width="120px" Height="20px" 
                                        ReadOnly="True"></asp:TextBox>



                                </td>



                            </tr>

                        </table>
                        <table>
        <tr>
        <td align="center" colspan=2 width="780px">



        </td>
        </tr>
        <tr>
        <td align="Left" colspan=3 style=" font-size:18Px; font-family:Times New Roman">
          data /////
        <br />

        <br />

        data//

         <br>
         <br />
         Please answer every question.
         <br />
         <br />


        </td>
        </tr>
        <tr >
        <td style="height: 20px; width: 550px;" align=center>
            <asp:Label ID="Label3" runat="server" 
                Text="question." Font-Bold="True" 
                Font-Size="20px" Font-Underline="True" ForeColor="Navy" 
                Font-Names="Times New Roman"></asp:Label>
        </td>
        <td style="height: 20px; width: 240px;" align=center colspan="2">
        <asp:Label ID="Label4" runat="server" 
                Text="Click Your Response" Font-Bold="True" 
                Font-Size="20px" Font-Underline="True" ForeColor="Navy" 
                Font-Names="Times New Roman"></asp:Label>
        </td>
        </tr>
        <tr>
        <td align="Left" style=" font-size:18Px; font-family:Times New Roman" height="45px">
        1. hi do you have car 

        </td>
        <td align="center" width="100px" height="35px">
            <asp:RadioButton ID="RadioButton1" CssClass="scaledRadioButton" runat="server" GroupName="Question1" 
                Text="Yes" Font-Size="12px" Height="20px" Width="49px"/>
            </td> 
            <td align="left"  width="100px" height="45px"> 
             <asp:RadioButton ID="RadioButton2" runat="server"
                    GroupName="Question1" Text="No" 
                    Font-Size="12px" CssClass="scaledRadioButton" Height="20px" Width="49px"/>
       </td>

        </tr>
         </table>





    </div>
    </form>
</body>
</html>

在代码后面我绑定了加载中数据库端的值 像这样

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim xmldoc As New XmlDocument
                    Dim xmldata As String = "<data><rdBx1>True</rdBx1></data>"
        Dim stringReader As New StringReader(kioskxml)
        xmldoc.Load(stringReader)
        stringReader.Dispose()

        If (XmlNodeNavigator.InitializeXmlNodeNavigator(xmldata)) Then

            Dim Q1 As String = XmlNodeNavigator.RetrieveElementValue("//data/rdBx1")
            If (Q1 = "True") Then
                RdbtnQ1Yes.Checked = True

            Else
                RdBtnQ1No.Checked = True
            End If


        End If

    End Sub

现在我想在数据库中单击按钮时将此数据绑定页面保存为html 谢谢

2 个答案:

答案 0 :(得分:2)

如果我了解更多或更少,必须是页面的渲染方法

string GetInnerHtml()
    {
        using (System.IO.StringWriter sw = new System.IO.StringWriter())
        {
            using (HtmlTextWriter htmlWriter = new HtmlTextWriter(sw))
            {
                Page.RenderChildren(htmlWriter);
                return sw.ToString();
            }
        }
    }

VB.NET版

    Function GetInnerHtml() As String
        Using sw As New IO.StringWriter
            Using htmlWriter As New HtmlTextWriter(sw)
                Page.RenderChildren(htmlWriter)
                Return sw.ToString()
            End Using
        End Using
    End Function

如何使用

public partial class _Default : Page
{

    protected override void Render(HtmlTextWriter writer)
    {
        var html = GetInnerHtml();

         //Use html string

        Response.Write(html);
    }


    string GetInnerHtml()
    {
        using (System.IO.StringWriter sw = new System.IO.StringWriter())
        {
            using (HtmlTextWriter htmlWriter = new HtmlTextWriter(sw))
            {
                RenderChildren(htmlWriter);
                return sw.ToString();
            }
        }
    }
}

答案 1 :(得分:1)

使用vb.net,您可以使用以下代码获取页面/网址的前端代码:

    Dim sourceString As new StringBuilder 
    sourceString.Append(CStr(New System.Net.WebClient().DownloadString(Request.PhysicalPath)))

Request.PhysicalPath将提供当前查看页面的地址