C# Sending GridViews/DataTables via Email

牧云@^-^@ 提交于 2019-12-11 16:21:59

问题


I'm trying the find the best way to send a GridView or DataTable in an email.

Page Behind Code:

protected void Page_Load(object sender, EventArgs e)
{
DataTable s1 = Sql.specificReportData(Convert.ToInt32(Session["userID"]));
this.gv.DataSource = s1.DefaultView;
this.gv.DataBind();
}

This generates and binds the data successfully, but if I try and add the contents of gv to a HTML encoded email then the gv part of the email is blank. Do I need to alter the GridView so it's HTML compliant? I can't find an example of how to do this. Any help appreciated.

edit: Gave answer to Solairaya as he gave fuller example, as well as object flushing and disposal. Marked both answers up as they both helped


回答1:


Page behind code

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = getHTML(GridView1);
    }

    private string getHTML(GridView gv) 
    { 
        StringBuilder sb = new StringBuilder(); 
        StringWriter textwriter = new StringWriter(sb); 
        HtmlTextWriter htmlwriter = new HtmlTextWriter(textwriter); 
        gv.RenderControl(htmlwriter); 
        htmlwriter.Flush(); 
        textwriter.Flush(); 
        htmlwriter.Dispose(); 
        textwriter.Dispose(); 
        return sb.ToString(); 
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        return;
    }

Page code

<form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
            SelectCommand="SELECT [UserID], [Name], [Email] FROM [WEB_Users] WHERE ([Name] LIKE '%' + @Name + '%')">
            <SelectParameters>
                <asp:Parameter DefaultValue="%Moha%" Name="Name" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
    </form>



回答2:


Hai alex try this,

Try this (C#):

using System.IO; using System.Text; using System.Net.Mail;

private string GridViewToHtml(GridView gv)
{
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    gv.RenderControl(hw);
    return sb.ToString();
}

protected void SendMailButton_Click(object sender, EventArgs e)
{
    MailMessage mail = new MailMessage();
    mail.Body = GridViewToHtml(GridView1);
    mail.IsBodyHtml = true;
    ......
}
public override void VerifyRenderingInServerForm(Control control)
{

}



回答3:


In case stringbuilder doesn't do it, try the following code:

protected void btnConfirmation_Click(object sender, EventArgs e)
{
    int rowcount = 0;
    foreach (GridViewRow row in GridView2.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            rowcount++;
        }
    }

    string strHTML = "<html><STYLE='font-size: 12px;'>" + 
                "<body>" +
                "<table border='1'>" +
                "<thead>" +
                   "<tr>" +
                     "<th>OrderDetail ID</th>" +
                     "<th>Order ID</th>" +
                     "<th>User Name</th>" +
                     "<th>Product ID</th>" +
                     "<th>Quantity</th>" +
                     "<th>Unit Price</th>" +
                     "<th>Shipping Costs</th>" +
                  "</tr>" +
                "</thead>";
    for (int a = 0; a < rowcount; a = a + 1)
    {
        strHTML = strHTML + "<tr>";
        for (int b = 0; b < 7; b = b + 1)
            {
                strHTML = strHTML + "<td>" + GridView2.Rows[a].Cells[b].Text + "</td>";
            }
    }
     strHTML = strHTML + "</tr></table>";
     strHTML = strHTML + "<br/><br/><br/>" +
               "<table>" +
               "<tr><td> Order ID <td/><td> " + GridView3.Rows[0].Cells[0].Text +
               "<tr><td> Order Date <td/><td> " + GridView3.Rows[0].Cells[1].Text +
               "<tr><td> User Name <td/><td> " + GridView3.Rows[0].Cells[2].Text +
               "<tr><td> Firstname <td/><td> " + GridView3.Rows[0].Cells[3].Text +
               "<tr><td> Lastname <td/><td> " + GridView3.Rows[0].Cells[4].Text +
               "<tr><td> Address <td/><td> " + GridView3.Rows[0].Cells[5].Text +
               "<tr><td> City <td/><td> " + GridView3.Rows[0].Cells[6].Text +
               "<tr><td> State <td/><td> " + GridView3.Rows[0].Cells[7].Text +
               "<tr><td> PostalCode <td/><td> " + GridView3.Rows[0].Cells[8].Text +
               "<tr><td> Country <td/><td> " + GridView3.Rows[0].Cells[9].Text +
               "<tr><td> Phone <td/><td> " + GridView3.Rows[0].Cells[10].Text +
               "<tr><td> Email <td/><td> " + GridView3.Rows[0].Cells[11].Text +
               "<tr><td> Total <td/><td> " + GridView3.Rows[0].Cells[12].Text +
               "<tr><td> Payment Transaction ID <td/><td> " + GridView3.Rows[0].Cells[13].Text +
               "<tr><td> Has Been Shipped <td/><td> " + GridView3.Rows[0].Cells[14].Text;
     string strTo = "xxx@telenet.be";
     string strSubject = "Expedition.";
     SendingMail(strHTML, strTo, strSubject);
}

protected void btnExpedition_Click(object sender, EventArgs e)
{
    string Firstname = GridView3.Rows[0].Cells[3].Text;
    string Lastname = GridView3.Rows[0].Cells[4].Text;
    string Address = GridView3.Rows[0].Cells[5].Text;
    string City = GridView3.Rows[0].Cells[6].Text; 
    string State = GridView3.Rows[0].Cells[7].Text;
    string PostalCode = GridView3.Rows[0].Cells[8].Text;
    string Country = GridView3.Rows[0].Cells[9].Text;
    string strSpaces = "&#32; &#32; &#32; &#32; &#32; &#32; &#32; &#32; &#32; &#32; &#32; &#32; &#32; &#32; &#32; &#32;";
    string html = "<!DOCTYPE html>" +
                  "<html lang='en'>" +
                  "<body><meta charset='utf-8' />" +
                  "<div><label>xxx</label></div>" +
                  "<div><label>xxxxxxxxxxxx</label></div>" +
                  "<div><label>xxxxxxxxxxxx</label></div>" +
                  "<div><label>xxxxxxx</label></div>" +
                  "<br/><br/>" +
                  "<div style='Font-size: 32px;'>" +
                  "<label>" + strSpaces + Firstname + " " + Lastname + "</label></div>" +
                  "<div style='Font-size: 32px;'>" +
                  "<label >" + strSpaces + Address + "</label></div>" +
                  "<div style='Font-size: 32px;'>" +
                  "<label>" + strSpaces + PostalCode + City + "</label></div>" +
                  "<div style='Font-size: 32px;'>" +
                  "<label>" + strSpaces + Country + "</label></div>" +
                  "</body>" +
                  "</html>";
    string strTo = "xxx@xxxxx";
    string strSubject = "Expedition.";
    SendingMail(html, strTo, strSubject);

}

protected void SendingMail(string strHTML, string strTo, string strSubject)
{
    MailAddress from = new MailAddress("alefever@telenet.be");
    MailAddress to = new MailAddress(strTo);
    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(from, to);
    mail.Subject = strSubject;

    mail.IsBodyHtml = true; 

    string url = HttpContext.Current.Request.Url.AbsoluteUri;
    mail.Body = strHTML;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.xxxx.xxx";
    smtp.Port = 587;

    smtp.Credentials = new NetworkCredential("xxx@telenet.be", "xxx5Qq");
    smtp.EnableSsl = true;

    smtp.Send(mail);

}

Tables:

Mail:



来源:https://stackoverflow.com/questions/1900128/c-sharp-sending-gridviews-datatables-via-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!