发送aspx页面与GridView的电子邮件 [英] Sending aspx page with gridviews as email

查看:321
本文介绍了发送aspx页面与GridView的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人递给我一个新的要求,这一个是新的我。一两天的研究后,我仍然摸不清这一个。要求是要找到一种方法,把我们的报告页面的电子邮件。该报告页面有多个GridView的,并连接到它的多个SqlDataSources。这页上的ASP控制很重。此页面确实属于使用Forms身份验证一个网站。我是能够使这个页面视图,能够未经授权的用户。我建议只发送链接的网页,每个人,即使他们没有登录,都能够看到的页面。这个想法似乎漏掉了像现在打印此页,扫描中它作为一个PDF格式,然后用电子邮件发送。现在看来似乎是击败的目的。我已经把一个发送电子邮件按钮aspx页面内点击我想这个页面发送的电子邮件的正文。这是我到目前为止已经试过..

I was handed a new requirement and this one is new to me. After a day or two of research, I'm still unable to figure this one out. The requirement is to find a way to send our "Report" page as an email. This report page has multiple gridviews and multiple SqlDataSources connected to it. This page is very heavy on the asp controls. This page does belong to a website that uses Forms authentication. I was able to make this page view-able to unauthenticated users. I recommended just sending the link to the page and everyone, even if they don't have a login, are able to see the page. This idea seemed to be over looked as they are now printing this page, scanning it in as a pdf, then emailing it. It seems like it is defeating the purpose. I have placed a "Send as Email" button inside the aspx page and one click I'm trying to send this page as the body of the email. This is what I have tried so far..

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页面。下面是我想要在电子邮件与所包含的按钮aslong身体派单GridView的例子....

This sends the email, but unfortunately, it only sends a single word which is "Back" and that is the link button that is in my aspx page. Here is a single gridview example of what I'm trying to send in the body of the email aslong with the buttons that are included....

    <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>

我也不太清楚,我错过了什么,或者如果它甚至有可能这个网页发送的电子邮件的正文。如果需要更多的信息,让我知道!谢谢!

I'm not too sure what I'm missing, or if it is even possible to send this page as the body of the email. If more information is needed, let me know! Thanks!

编辑:使用塞恩普拉迪普的建议后,我收到了一个错误。错误是..

After using Sain Pradeep suggestion, I was receiving an error. The error was..

型'的GridView'控制'FeaturedContent_gvAlexandria'必须放在与RUNAT =服务器的表单标签内。

"Control 'FeaturedContent_gvAlexandria' of type 'GridView' must be placed inside a form tag with runat=server."

要解决这个问题,我插..

To fix this, I inserted..

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 + = GetGridviewData(gvAlexandria)删除了从按钮点击使用,取而代之的sendMail.Body = reader.ReadToEnd(),并添加一个用于每个gridview的。所有的GridView的现在在发送的电子邮件。再次感谢所有帮助!

Which overrides the exception and sends the email correctly. I also removed the "Using" from the button click and replaced sendMail.Body = reader.ReadToEnd() with sendMail.Body += GetGridviewData(gvAlexandria) and added one for each gridview. All gridviews now send in the email. Thanks again for all the help!

推荐答案

请做这样的

Msg.Body += GetGridviewData(gvUserInfo);

功能gridview的数据转换

function to convert gridview data

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

这篇关于发送aspx页面与GridView的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆