Skip to content Skip to sidebar Skip to footer

How Can I Copy HTML ASP.NET VB Form/Table To Send As Email

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

Solution 1:

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   

Solution 2:

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();
}

Post a Comment for "How Can I Copy HTML ASP.NET VB Form/Table To Send As Email"