Emailing Rendered Output Fails in ASP.NET 2.0

Posted on 2008-11-12
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

    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

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    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…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now