There are times you want to send an ASP.NET GridView along with data as an Email. Your recipient should receive it with the styles you have applied to the GridView inside your ASP.NET web page. This post is supposed to explain the way to achieve such a task.
First I will be creating an empty ASP.NET web application. I have nothing but a empty Web Form added to the application which I named as “Default”. Now in the code behind of the Web Form, I have following helper method which will return me a DataTable holding some data which I need to be shown in the GridView.
There is noting complex in the above method, just creating a DataTable with some columns, filling up some data and then returning the DataTable.
In the Web Form, I have the following design.
Here there is GridView inside a Form tag, I have applied some styles to keep my GridView look nicer. Now in the Page_Load event of my Web Form, I am binding the GridView to the DataTable which is received from above GetData() method. Now when I run the application, I am getting the following page.
To create the mail message and to send it I have the following method.
Above method accepts a string and that particular string will be sent as the body in my MailMessage. And please note here in my MailMessage, I am setting up the property IsBodyHtml to true. That’s because I am planning to retrieve the content of my GridView as a markup characters using the GridView.RenderControl Method. And that particular string will be the body of my MailMessage. After doing that, I am creating a SmtpClient to send the email through Gmail SMTP.
Now in my Send button click I have the following.
Here GridView.RenderControl Method (HtmlTextWriter) Outputs server control content to the provided HtmlTextWriter object. StringWriter is required for the HtmlTextWriter to write to. It is a buffer and everything that is written to HtmlTextWriter is written to StringWriter. Then I am passing content in StringWriter to my SendGridViaEmail() method. Here you can either use,
But HtmlTextWriter.InnerWriter Property will be eventually giving you the reference to underlying writer which is the used StringWriter.
Now when I run the application and click on Send button, I am getting this nice little error message which is “Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server. ”.
|Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.|
I am sure my control 'GridView1' is placed inside a form tag with runat=server. But the problem is when we render the control using GridView.RenderControl Method, the markup will be rendered without the form tag. You can simple avoid this error by adding the following method. This Page.VerifyRenderingInServerForm Method confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.Now when I build and run the application and click on Send button, I am not getting any errors, instead I am getting the desired output, an email sent with styled GirdView along with data.