C#通过电子邮件发送GridViews / DataTables [英] C# Sending GridViews/DataTables via Email

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

问题描述

我试图找到在电子邮件中发送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提供了一个更全面的例子,以及物体冲洗和处理的答案。标记两个答案,因为他们都帮助

p $ p $ $
$ b $ Label1Text = getHTML(GridView1); pre $ protected void Button1_Click(object sender,EventArgs e)


私有字符串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 =form1runat =server> 
< div>
< asp:GridView ID =GridView1runat =serverAutoGenerateColumns =FalseDataSourceID =SqlDataSource1>
<列>
< asp:BoundField DataField =UserIDHeaderText =UserIDSortExpression =UserID/>
< asp:BoundField DataField =NameHeaderText =NameSortExpression =Name/>
< asp:BoundField DataField =EmailHeaderText =EmailSortExpression =Email/>
< /列>
< / asp:GridView>
< asp:SqlDataSource ID =SqlDataSource1runat =serverConnectionString =<%$ ConnectionStrings:DBConnectionString%>
SelectCommand =SELECT [UserID],[Name],[Email] FROM [WEB_Users] WHERE([Name] LIKE'%'+ @Name +'%')>
< SelectParameters>
< asp:参数DefaultValue =%Moha%Name =NameType =String/>
< / SelectParameters>
< / asp:SqlDataSource>
< asp:Button ID =Button1runat =serverOnClick =Button1_ClickText =Button/>
< br />
< br />
< asp:Label ID =Label1runat =serverText =Label>< / asp:Label>< / div>
< / form>


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

解决方案

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>

这篇关于C#通过电子邮件发送GridViews / DataTables的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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