通过电子邮件发送带有网格视图的 aspx 页面
本文关键字:视图 网格 aspx 页面 电子邮件 | 更新日期: 2023-09-27 18:32:59
我收到了一个新要求,这个对我来说是新的。经过一两天的研究,我仍然无法弄清楚这一点。要求是找到一种方法将我们的"报告"页面作为电子邮件发送。此报表页具有多个网格视图和连接到它的多个 SqlDataSources。此页在 asp 控件上非常繁重。此页面属于使用表单身份验证的网站。我能够使未经身份验证的用户能够查看此页面。我建议只发送指向该页面的链接,每个人,即使他们没有登录名,也可以看到该页面。这个想法似乎被忽视了,因为他们现在正在打印此页面,将其扫描为pdf,然后通过电子邮件发送。似乎它正在破坏目的。我在aspx页面内放置了一个"作为电子邮件发送"按钮,单击一下我正在尝试将此页面作为电子邮件的正文发送。这就是我到目前为止尝试过的。.
protected void btnEmail_Click(object sender, EventArgs e)
{
using (System.IO.StreamReader reader = System.IO.File.OpenText(Server.MapPath("~/Reporting/Report.aspx")))
{
string fromAddress = "fromaddress@something.com";
string toAddress = "toaddress@something.com;
System.Net.Mail.MailMessage sendMail = new System.Net.Mail.MailMessage(fromAddress, toAddress);
sendMail.Subject = "Testing";
sendMail.IsBodyHtml = true;
sendMail.Body = reader.ReadToEnd();
SmtpClient smtp = new SmtpClient("mail.something.com");
smtp.Send(sendMail);
}
}
这会发送电子邮件,但不幸的是,它只发送一个单词"返回",即我的aspx页面中的链接按钮。这是一个单一的网格视图示例,说明我尝试在电子邮件正文中发送的内容,只要包含的按钮......
<script lang="javascript" type="text/javascript">
function printPage() {
document.getElementById('<%= btnPrint.ClientID %>').style.display = 'none';
document.getElementById('<%= lbBack.ClientID %>').style.display = 'none';
document.getElementById('<%= btnEmail.ClientID%>').style.display = 'none';
window.print();
document.getElementById('<%= btnPrint.ClientID %>').style.display = 'none';
document.getElementById('<%= lbBack.ClientID %>').style.display = 'none';
document.getElementById('<%= btnEmail.ClientID%>').style.display = 'none';
}
</script>
<div class="content-wrapper">
<asp:LinkButton ID="lbBack" runat="server" OnClientClick="JavaScript:window.history.back(1);return false;">Back</asp:LinkButton>
<asp:Button ID="btnPrint" runat="server" Text="Print" Font-Size="X-Small" Height="27px" Width="44px" OnClientClick="printPage()" />
<asp:Button ID="btnEmail" runat="server" Font-Size="X-Small" Height="27px" OnClick="btnEmail_Click" Text="Send as Email" Width="105px" />
</div>
<div class="content-wrapper">
<asp:Label ID="lblAlexandria" runat="server" Text="Alexandria" Font-Bold="True" Font-Size="Large"></asp:Label>
</div>
<div class="total-header" style="text-align: right">
<asp:Label ID="lblTotalAlexandria" runat="server" Text="Total" BackColor="Black" ForeColor="White" Font-Bold="true"></asp:Label>
</div>
<asp:GridView ID="gvAlexandria" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="AlexandriaDataSource" GridLines="None" PageSize="200" HorizontalAlign="Center" ShowFooter="True" OnRowDataBound="gvAlexandria_RowDataBound">
<Columns>
<asp:BoundField DataField="Dealership" HeaderText="Dealership" SortExpression="DEALER NAME" Visible="False">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="150px" BackColor="Black" ForeColor="White" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="DealDate" DataFormatString="{0:MM/dd/yyyy}" HeaderText="DealDate" SortExpression="DealDate">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="150px" BackColor="Black" ForeColor="White" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Location" HeaderText="Status" SortExpression="Location" Visible="False">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="150px" BackColor="Black" ForeColor="White" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField HeaderText="Bounced" SortExpression="Bounced" DataField="Bounced">
<FooterStyle />
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="150px" BackColor="Black" ForeColor="White" />
<ItemStyle HorizontalAlign="Left" ForeColor="#CC0000" />
</asp:BoundField>
<asp:BoundField DataField="StockNumber" HeaderText="StockNumber" SortExpression="STOCK NO">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="150px" BackColor="Black" ForeColor="White" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Buyer" HeaderText="Buyer" SortExpression="LAST NAME">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="150px" BackColor="Black" ForeColor="White" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField HeaderText="Reason" SortExpression="Reason" DataField="Reason">
<HeaderStyle Width="150px" BackColor="Black" ForeColor="White" HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField HeaderText="AmtFinanced" SortExpression="AmtFinanced">
<ItemTemplate>
<asp:Label ID="lblAmtFinanced" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "AmtFinanced","{0:C}") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<%--<asp:Label ID="lblTotal" runat="server" Text="Total" BackColor="Black" ForeColor="White" Font-Bold="true"></asp:Label>--%>
</FooterTemplate>
<HeaderStyle BackColor="Black" ForeColor="White" />
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="D.O">
<ItemTemplate>
<asp:Label ID="lblDaysOut" runat="server" Text='<%# Eval("DaysOut") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle BackColor="Black" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" Width="60px" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
</Columns>
</asp:GridView>
我不太确定我错过了什么,或者是否可以将此页面作为电子邮件的正文发送。如果需要更多信息,请告诉我!谢谢!
编辑:使用Sain Pradeep建议后,我收到错误。错误是..
"类型为'GridView'的控件'FeaturedContent_gvAlexandria'必须放置在具有runat=server的表单标记中。
为了解决这个问题,我插入了..
public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
这将覆盖异常并正确发送电子邮件。我还从按钮单击中删除了"使用",并替换了sendMail.Body = reader。ReadToEnd(( with sendMail.Body += GetGridviewData(gvAlexandria(,并为每个网格视图添加一个。所有网格视图现在都发送电子邮件。再次感谢所有的帮助!
请这样做
Msg.Body += GetGridviewData(gvUserInfo);
转换网格视图数据的函数
// This Method is used to render gridview control
public string GetGridviewData(GridView gv)
{
StringBuilder strBuilder = new StringBuilder();
StringWriter strWriter = new StringWriter(strBuilder);
HtmlTextWriter htw = new HtmlTextWriter(strWriter);
gv.RenderControl(htw);
return strBuilder.ToString();
}
您将无法在电子邮件中发送任何"asp"代码并将其正确显示在另一端,您需要一个Web服务器才能将asp代码转换为屏幕上的可读格式。
您关于拥有一个单独的、不安全的页面的想法是正确的。
另一个想法是将 html"打印"到图像并发送。
另一种选择是生成一个输出与电子邮件客户端兼容的 html 的页面,并将其作为正文通过电子邮件发送。
为什么不使用wkhtmltopdf c#包装器或EvoPDF等工具将页面转换为PDF并通过电子邮件作为附件?
请注意:wkhtmltopdf c# 库是免费的,而 EvoPDF 不幸的是是一个商业产品。