简体   繁体   中英

gridview.rendercontrol returns System.Web.UI.HtmlTextWriter

I am trying to send grid in my email, It sends email with a only System.Web.UI.HtmlTextWriter , It does not populate grid.

here is my code:

 System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
 System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();

 StringBuilder emailBody = new StringBuilder(); 
 StringWriter emailStringWriter = new StringWriter();
 HtmlTextWriter htmlGrid = new HtmlTextWriter(new HtmlTextWriter(new StringWriter(emailBody, CultureInfo.InvariantCulture)));

 mail.Dispose();
 mail = new System.Net.Mail.MailMessage();
 mail.To.Add('abc@xyz.com');
 mail.From = 'def@yzy.com';
 mail.Subject = 'grid example';

 myGrid.RenderControl(htmlGrid);
 mail.Body = htmlGrid.ToString();
 mail.IsBodyHtml = true;
 smtp = new System.Net.Mail.SmtpClient(ConfigurationManager.AppSettings["smtpSetting"].ToString());
 smtp.Port = 25;
 smtp.Send(mail);
 mail.Dispose();

I received the email but grid was not there , only this text was there: System.Web.UI.HtmlTextWriter

You need to declare a StringBuilder for your HtmlTextWriter.

StringBuilder sb = new StringBuilder();
HtmlTextWriter htmlGrid  = new HtmlTextWriter(new StringWriter(sb, CultureInfo.InvariantCulture));  

You may need to flush the writer(not certain), but you will use sb.ToString() to get the rendered html.

mail.Body = sb.ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM