Output asp.net masterpage web form to html email

Hi, I have a little nightmare here!

I am trying to output webform in html using page.rendercontrol and htmltextwriter, but the result is an empty letter.

the code:

StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); HtmlTextWriter htmlTW = new HtmlTextWriter(sw); page.RenderControl(htmlTW); String content = sb.ToString(); MailMessage mail = new MailMessage(); mail.From = new MailAddress(" test@test.com "); mail.To.Add(" steve@test.com "); mail.IsBodyHtml = true; mail.Subject = "Test"; mail.Body = content; SmtpClient smtp = new SmtpClient("1111"); smtp.Send(mail); Response.Write("Message Sent"); 

I also tried this by creating a single text box and receiving an error message. It should be in the form tags (which are on the main page).

I tried this fix: http://forums.asp.net/p/1016960/1368933.aspx#1368933 and added:

 public override void 

VerifyRenderingInServerForm (control) {return; }

But now I get the following:

VerifyRenderingInServerForm (Control) ': no ​​suitable method found for overriding

Does anyone have a fix? I tear off my hair!

Thanks,

Steve

+4
source share
5 answers

This is what I do in vb:

 Dim sw as New StringWriter() Dim writer as New HtmlTextWriter(sw) System.Web.HttpContext.Current.Server.Execute("YourPage.aspx", writer) Dim message as String = sw.ToString() 
+4
source

If you don’t need to redirect the page after rendering the contents of the page (from your sample code this doesn’t look like what you need), then you can use Response.Filter.

On top of my head, it looks something like this:

 protected void Page_Load(object sender, System.EventArgs e) { Response.Filter = new SmtpFilter(Response.Filter); } 

The SmtpFilter class is just a class that inherits from the Stream object.

The main method will be the Write method. Here is some code from my head to override the Write (...) method, send Smtp mail and continue processing.

  public override void Write(byte[] buffer, int offset, int count) { // get the html string content= System.Text.Encoding.UTF8.GetString(buffer, offset, count); MailMessage mail = new MailMessage(); mail.From = new MailAddress(" test@test.com "); mail.To.Add(" steve@test.com "); mail.IsBodyHtml = true; mail.Subject = "Test"; mail.Body = content; SmtpClient smtp = new SmtpClient("1111"); smtp.Send(mail); buffer = System.Text.Encoding.UTF8.GetBytes(HTML); this.Base.Write(buffer, 0, buffer.Length); } 

If you need more help with Response.Filters, you might want to google it. The first article I found was in VB.NET but is still useful:

http://aspnetlibrary.com/articledetails.aspx?article=Use-Response.Filter-to-intercept-your-HTML

+1
source

So, I struggled with this and FINALLY found the answer ...

I tried to extract the displayed HTML GridView file and put it in an email. Everything works fine when this action happens on the page, but when I moved the functionality to User Control, the VerifyRenderingInServerForm method no longer works because it is a page method, not a control method.

To fix this, you need to override the Render method and invoke the page to ensure that the control displays in the form with the runat = server tag. Here is the fix ...

 protected override void Render(HtmlTextWriter writer) { if (Page != null) { Page.VerifyRenderingInServerForm(this); } base.Render(writer); } 

Here's a WASTE time, but finally!

+1
source

This is the method I use to drag and drop my web forms into an email:

 private string HtmlPageInToString() { WebRequest myRequest; myRequest = WebRequest.Create(@"http://yoururlhere/"); myRequest.UseDefaultCredentials = true; WebResponse myResponse = myRequest.GetResponse(); Stream ReceiveStream = myResponse.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); StreamReader readStream = new StreamReader(ReceiveStream, encode); return readStream.ReadToEnd(); } 

This will fill your web form in line to use it as you would like. The great thing is that it pulls the entire page, so you don’t have to worry about the tags from your main page also not being included.

0
source

It seems to me that you can do this with a user control and then visualize the user control output into a string using the following code:

 public class ViewManager { public static string RenderView(string path, object data) { Page pageHolder = new Page(); UserControl viewControl = (UserControl) pageHolder.LoadControl(path); if (data != null) { Type viewControlType = viewControl.GetType(); FieldInfo field = viewControlType.GetField("Data"); if (field != null) { field.SetValue(viewControl, data); } else { throw new Exception("ViewFile: " + path + "has no data property"); } } pageHolder.Controls.Add(viewControl); StringWriter result = new StringWriter(); HttpContext.Current.Server.Execute(pageHolder, result, false); return result.ToString(); } 

This code fires all the usual events in the control, and you can load the published form data into the via property of the control.

This code was removed from a post by Scott Guthries.

Hi

Jesper Hauge

0
source

Source: https://habr.com/ru/post/1304560/


All Articles