send rendered GridView in an email - asp.net

I have a GridView whose HTML I need to put into an email.
How can this be done?

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

Related

Creating a report from label values in asp.net

I have a website that I wrote in ASP.net and I have a page called Statistics.aspx
that I displayed few useful numbers that admin can view in labels .
I want to make a button " Download Report " that can put these numbers into a word/pdf document .
Can anyone help ?
You need to give a bit more detail.Well I believe this is useful
string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
StringBuilder sb = new StringBuilder();
sb.AppendLine(Label1.text); // and so on
using (StreamWriter outfile = new StreamWriter(mydocpath + #"\AllTxtFiles.txt"))
{
outfile.Write(sb.ToString());
}

how to send gridview containing image via email using asp.net c#

I have a gridview that contain image . I want to email that grid containing image. I have a code that send the gridview via email but the image in gridview are not visible at the receiver email id. The image in gridview are displayed from the database. can anyone help me to send gridview with its formatting via email.
public string gridData(GridView gv)
{
StringBuilder strBuilder = new StringBuilder();
StringWriter strWriter = new StringWriter(strBuilder);
HtmlTextWriter htw = new HtmlTextWriter(strWriter);
gv.RenderControl(htw);
return strBuilder.ToString();
}

How to create a dynamic table from java script?

How to create a dynamic table from java script?I am fetching a record from database and storing into variable and adding that value to table cell.
Here I put some text not actual value.
In cs file's button click event I doing like this
StringBuilder sb = new StringBuilder();
sb.Append(#"<script language='javascript'>");
sb.Append("<table><tbody><tr><td>Information</td></tr></tbody></table>");
sb.Append(#"</script>");
if (!Page.ClientScript.IsClientScriptBlockRegistered(Page.GetType(), "HeyPopup"))
ClientScript.RegisterClientScriptBlock(Page.GetType(), "HeyPopup", sb.ToString());
But I am unable to see Information.
you must add document.Write in your script
so replace with
sb.Append("document.write('<table><tbody><tr><td>Information</td></tr></tbody></table>');");

Generate Compressed HTML From GridView Control in Asp.Net

Is there any Possibility that it can Generate Compressed HTML from the GridVIew ???
I do not suggest to do that, but I can give an idea for how I will try to do that:
You can render the GridView in a string, make the compression and then show it to the page.
TextWriter stringWriter = new StringWriter();
HtmlTextWriter GrapseMesaMou = new HtmlTextWriter(stringWriter);
cGridView.RenderControl(GrapseMesaMou);
// this is the string that you show on page (eg place it on a literal)
string cFinalResults = CompressHtml(stringWriter.ToString());
// not show it any more...
cGridView.Visible = false;
one html compressor: http://code.google.com/p/htmlcompressor/
I do not know if this can work smoothly for all cases, but you can give it a try to see if it is what you look for.

Render ASP.Net PlaceHolder .ToString(), not to page

I've searched around and have not been able to find an good solution. I have a custom extension to a PlaceHolder control that will contain expressions that I would like to take the string output of without having to call control.Render(), since that call automatically writes the contents out to the page.
Does anybody know how to get the would be rendered content into a string and prevent the page from containing it?
The often-regurgitated, slightly dated code for this goes something like:
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();
}

Resources