How can I copy HTML ASP.NET VB Form/Table to send

2019-08-16 10:27发布

I have a few large, specifically formatted to the customer's request, tables with input. It looks similar to the following...

<body id="Body" class="Window" runat="server">
    <form id="Form" runat="server" defaultbutton="SubmitLinkButton">
        <!-- Markup for a the SubmitLinkButton and DropDownList -->
        <!--    to pick which Table is shown                    -->
        <asp:Table ID="Table1" runat="server">
            <asp:TableRow class="row" runat="server">
                <asp:TableCell runat="server">
                    <pre>    Some Input1    </pre>
                    <pre>___________________</pre>
                    <pre>|___<asp:Textbox ID="Textbox1" runat="server"></asp:Textbox>____|</pre>
                    <pre>|_________________|</pre>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table ID="Table2" runat="server">
            <asp:TableRow class="row" runat="server">
                <asp:TableCell runat="server">
                    <pre>    Some Input2    </pre>
                    <pre>___________________</pre>
                    <pre>|___<asp:Textbox ID="Textbox2" runat="server"></asp:Textbox>____|</pre>
                    <pre>|_________________|</pre>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
    </form>
</body>

Underwhelming, right?

Only one of the four tables is visible or not depending on the selection chosen in the DropDownList. Each table has upwards of 30-40 inputs and each area with inputs is formatted uniquely. All formatted the same way (^^^like above^^^), but one may have a section with 3 inputs and lots of text or 8 inputs and little text or no inputs and just a section of text.

Hopefully all of that makes sense.

What I need to figure out is how to have the user be able to "Submit" the form via the SubmitLinkButton which will send an email that looks identical to the form they filled out to a group of email addresses setup in the SystemFramwork.config.

I've attempted to do this, using Visual Basic, with RenderControl(), but I kept getting errors saying my Textboxes needed to be inside a form with runat="server" in it, and as you can see in my code above I have that. So, I'm not sure what was going on there.

Since the form is formatted so customized, If I can't somehow render the HTML form from page to email to have them look identical, I don't know any other option than to add the markup to the email manually, which just seems like a waste of time and making redundancies in the project.

Any insight would be greatly appreciated!

I'm still currently working with a pseudo solution that looks something like this...

Public Sub SubmitLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitLinkButton.Click
    Dim result As String = vbNull

    Dim stringWriter As New StringWriter()
    Dim htmlWriter As New HtmlTextWriter(stringWriter)

    'If the user selected something with the DropDown
    If (DDL_Selection IsNot "")
        Dim email As New MailMessage(FromConfigVar, ToConfigVar)
        email.Subject = DDL_Selection.SelectedValue & " Table"
        email.IsBodyHtml = True

        Select Case DDL_Selection
            Case "Table1"
                Try
                    htmlWriter.RenderBeginTag(HtmlTextWriterTag.Table)
                    Table1.RenderControl(htmlWriter)
                    htmlWriter.RenderEndTag()
                    htmlWriter.Flush()

                    result = stringWriter.ToString()
                Finally
                    htmlWriter.Close()
                    stringWriter.Close()
                End Try
        End Select
        mailMessage.Body = result
    Else
        'Do nothing
    End If
End Sub

Again, this solution is not working, nor do I think I'm even close to being on the right track. Just thought I'd show what I've tried.

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-16 11:06

I got this version working but did not get any user-input returned. This puts the html into an email; uses HtmlAgilityPack.

using HtmlAgilityPack;
etc.

protected void btnTableToEmail_Click(object sender, EventArgs e)
{
    try
    {
        StringWriter sw = new StringWriter();
        using(HtmlTextWriter writer = new HtmlTextWriter(sw))
        {
            writer.AddAttribute("runat", "server");
            writer.RenderBeginTag("form");

            writer.Write(GetTableHTML());

            writer.RenderEndTag();
        }

        SendEmail(sw);
    }
    catch(Exception)
    {
        throw;
    }
}

private string GetTableHTML()
{
    // uses HtmlAgilityPack.

    var html = new HtmlDocument();
    html.Load(Server.MapPath("~/yourpage.aspx")); // load a file
    var root = html.DocumentNode;

    var table = root.Descendants().Where(n => n.GetAttributeValue("id", "").Equals("Table1")).Single();
    return table.InnerHtml;
}

private void SendEmail(StringWriter sw)
{
    // your email routine.
    // ...
    msg.Body = sw.ToString();
}
查看更多
虎瘦雄心在
3楼-- · 2019-08-16 11:27

If you override the Page's VerifyRenderingInServerForm Method to not perform the validation that is causing the issue you can get around this problem:

'This is in the Page's Code Behind.
Public Overrides Sub VerifyRenderingInServerForm (control As Control)
    'Do Nothing instead of raise exception.
End Sub   
查看更多
登录 后发表回答