开发者

Output asp.net masterpage webform to html email

Hi I'm having a bit of a nightmare here!

I'mv trying output a webform to html using page.rendercontrol and htmltextwriter but it is resulting in a blank email.

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.F开发者_JAVA技巧rom = 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 it by rendering a single textbox and got and error saying It needed to be within form tags (which are on the masterpage).

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

public override void 

VerifyRenderingInServerForm(Control control) { return; }

But now the errror I get is:

VerifyRenderingInServerForm(Control)': no suitable method found to override

Does anyone have a fix for this? I'm tearing my hair out!!

Thanks,

Steve


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()


If you don't need to redirect the page, after you render the contents of the page (from your code sample, it doesn't look like you need to), then you may want to use a Response.Filter.

Off the top of my head, it would look something like:

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 off the top of my head to Override the Write(...) method, send the Smtp mail, and continue on 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 on Response.Filters, you may want to google it. The first article I found was in VB.NET, but still helpful:

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


So I was struggling with this and FINALLY found the answer...

I was trying to extract the rendered HTML of a GridView and place it in an email. Everything worked perfectly when that action was happening on a Page, but when I moved the functionality into a User Control, the VerifyRenderingInServerForm method no longer works because it is a Page method, not a Control method.

To fix this, you have to override the Render method and call up to the Page to insure that control is rendered in a form with a runat=server tag. Here is the fix...

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

Thats HOURS of wasted time right there, but finally!


This is the method I use to cram my webforms 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 stuff your webform into a string for use how ever you would like. The great thing is that it pulls in the whole page so you won't have to worry about the tags from your masterpage not being included also.


To me this sounds like you could do this with a user control, and then render 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 normal events in the control, and you can load the posted form data into the via a Data property in the control.

This code was lifted from Scott Guthries blog post here.

Regards

Jesper Hauge

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜