我正在尝试查找在电子邮件中发送GridView或DataTable的最佳方式。
代码背后的页面:
protected void Page_Load(object sender, EventArgs e)
{
DataTable s1 = Sql.specificReportData(Convert.ToInt32(Session["userID"]));
this.gv.DataSource = s1.DefaultView;
this.gv.DataBind();
}
这会成功生成并绑定数据,但如果我尝试将gv的内容添加到HTML编码的电子邮件中,则电子邮件的gv部分为空白。我是否需要更改GridView以使其符合HTML标准?我找不到如何做到这一点的例子。任何帮助表示赞赏。
编辑:回答Solairaya,因为他给出了更全面的例子,以及对象冲洗和处理。两个答案都标明了,因为他们都帮助了
答案 0 :(得分:5)
代码页面
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;
}
网页代码
<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>
答案 1 :(得分:1)
Hai alex试试这个,
Try this (C#):
使用System.IO; 使用System.Text; 使用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)
{
}
答案 2 :(得分:-1)
如果stringbuilder没有这样做, 尝试以下代码:
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 = "                               ";
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);
}
表:
邮件: