开发者

C# Sending GridViews/DataTables via Email

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 appreci开发者_运维问答ated.

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


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>


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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜