开发者

send rendered GridView in an email

I have a GridView whose HTML I need to put into an email.

How can this be don开发者_如何学Goe?


use an HTMLtext writer to render the gridview control.You will get an HTML output as string.use this as your message body.

public string RenderControl(Control ctrl) 
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);

ctrl.RenderControl(hw);
return sb.ToString();
}


Use a stringbuilder to create a HTML table to resample your gridview like so:

var output = new StringBuilder("<table cellpadding='5' style='border: solid 1px black;'>");
foreach (GridViewRow row in MyGridView.Rows)
{
    output.Append("<tr>");
    output.Append("<td>" + Title + "</td>");
    output.Append("<td>" + Price + "</td>");
    output.Append("<td>" + Quantity + "</td>");
    output.Append("</tr>");

}
output.Append("</table>");


Ideally you should not be concerned about the GridView control. Take the data that is being displayed in the control, format it in what ever manner you want and pass it as your message body.

I would treat the logic to display Grid in my application and generating grid for message body as two separate functionality. What will be common in between the two, is the source of data. For formatting of data(generating the Grid), you can use the HTMLTextWriter class(as mentioned by others) and format the data in what ever way you want.


Two thing you can do for this

1) take screen shot of the current page by using any utility and send in mail

2) you can export grid as excel and than sent it in mail.

Export grid to Excel http://geekswithblogs.net/azamsharp/archive/2005/12/21/63843.aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜