Emailing Rendered Output Fails in ASP.NET 2.0

Posted on 2008-11-12
Medium Priority
Last Modified: 2013-11-07
I am trying to email an entire page of controls in HTML format.  This worked flawlessly in 1.1, but after upgrading to 2.0 the rendercontrol command crashes with a "RegisterforEventValidation can only be called during Render().  The HTML side consists of a form that contains the panel "wholeform", and all objects on the page are enclosed in that.

I have already tried setting enableeventvalidation = false in the page directive, and experimenting
with the visibility property of different objects with no success.

Any help would be greatly appreciated.  Please be explicit, since I'm no expert.  Thanks.

Dim p As New Page       		'Create a new Page instance and a server-side form
        Dim hf As New HtmlForm
        p.Controls.Add(hf)  		'Build up the control hierarchy
        Dim SB As New StringBuilder
        Dim SW As New StringWriter(SB)
        Dim htmlTW As New HtmlTextWriter(SW)
        p.RenderControl(htmlTW)     'Call the page's RenderControl() method
        p = Nothing
       strEmailBody = SB.ToString()
	... code to email

Open in new window

Question by:KarenH1
LVL 15

Assisted Solution

NazoUK earned 1000 total points
ID: 22948681
There's an article over at 4 guys from Rolla that describes this problem:


I think the fact that you are trying to use a page variable will cause additional problems though because that will have its own render event, event validation etc. This is why disabling event validition isn't working in your case. I recommend using rendercontrol of wholeform directly instead of using the page variable. If you need additional html adding to the rendered output it should be trivial to do this as strings.

Accepted Solution

KarenH1 earned 0 total points
ID: 22978973
NazoUK - Thank you for your reply.  I got the original code from 4 guys from Rolla, and tried all their workarounds.  They admit this code breaks in 2.0.  I do believe I tried to do a renderconrol of the wholeform with the same results.

I think I have a solution:

Dim sbNewOne As New StringBuilder()
            Dim mywriter As New HtmlTextWriter(New System.IO.StringWriter(sbNewOne))
            ' Without this, get "a page can only have one server side form tag"
            MyBase.Visible = False

            strEmailBody = sbNewOne.ToString()

This seems to work no problem.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question