4

嗨,我在这里做了一个噩梦!

我正在尝试使用 page.rendercontrol 和 htmltextwriter 将 web 表单输出到 html,但它会导致一封空白电子邮件。

代码:

   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");

我还通过渲染单个文本框进行了尝试,并得到错误提示它需要在表单标签内(位于母版页上)。

我尝试了此修复:http ://forums.asp.net/p/1016960/1368933.aspx#1368933 并添加:

public override void 

VerifyRenderingInServerForm(Control control) { return; }

但现在我得到的错误是:

VerifyRenderingInServerForm(Control)':找不到合适的方法来覆盖

有没有人可以解决这个问题?我要拔头发了!!

谢谢,

史蒂夫

4

5 回答 5

4

这就是我所做的,在 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()
于 2010-03-19T13:43:42.017 回答
1

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

于 2010-03-20T19:39:39.077 回答
1

所以我一直在努力解决这个问题,终于找到了答案......

我试图提取 GridView 的呈现 HTML 并将其放入电子邮件中。当该操作发生在页面上时,一切都运行良好,但是当我将功能移到用户控件中时,VerifyRenderingInServerForm 方法不再起作用,因为它是页面方法,而不是控件方法。

要解决此问题,您必须重写 Render 方法并调用 Page 以确保控件以带有 runat=server 标记的表单呈现。这是修复...

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

那是浪费时间的几个小时,但最后!

于 2011-01-29T00:04:48.390 回答
0

对我来说,这听起来像是您可以使用用户控件执行此操作,然后使用以下代码将用户控件输出呈现为字符串:

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

此代码触发控件中的所有正常事件,您可以通过控件中的 Data 属性将发布的表单数据加载到 中。

这段代码是从 Scott Guthries的博客文章中提取的。

问候

杰斯珀豪格

于 2010-04-06T19:24:44.920 回答
0

这是我用来将我的网络表单塞进电子邮件的方法:

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

这会将您的网络表单填充到一个字符串中,以供您随意使用。最棒的是它会拉入整个页面,因此您不必担心母版页中的标签也不会被包含在内。

于 2010-03-23T15:37:18.213 回答