Emailing Rendered Output Fails in ASP.NET 2.0

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
KarenH1Author Commented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.