none
RegisterForEventValidation can only be called during Render

    Question

  • I get this error when sending a mailout from a web page. Mail using the same code works for single emails, but fails after an undetermined number of emails (>100) in a mailout.

    The same code worked for mailouts on a .Net 4.0 server. This error occurred after migrating to a .NET 4.5 server.

    The code and error trace follow.

                MailMessage email = new MailMessage();
                string fromEmail = localAgency.Email;
                email.From = new MailAddress(fromEmail, BusinessDetails.ShortName);
                email.To.Add(localAgency.Email);
                email.Subject = "Sent" + emailSubject;
                email.Body = sentCount + " emails sent with the following message:"
                + Environment.NewLine
                + emailbody;
                try
                {
                    object userState = email;
                    SmtpClient smtp = new SmtpClient();
                    smtp.SendAsync(email, userState);
                }
                catch
                {
                    throw;
                }
                Server.Transfer(Website.ControlPanelURL, true);
    
    System.Web.HttpException (0x80004005): Exception of type 'System.Web.HttpException' was thrown. ---> System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.InvalidOperationException: RegisterForEventValidation can only be called during Render();
       at System.Web.UI.ClientScriptManager.RegisterForEventValidation(String uniqueId, String argument)
       at System.Web.UI.ClientScriptManager.RegisterForEventValidation(PostBackOptions options)
       at System.Web.UI.ClientScriptManager.GetPostBackEventReference(PostBackOptions options, Boolean registerForEventValidation)
       at System.Web.UI.WebControls.LinkButton.AddAttributesToRender(HtmlTextWriter writer)
       at System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter writer)
       at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.WebControls.LoginStatus.RenderContents(HtmlTextWriter writer)
       at System.Web.UI.WebControls.LoginStatus.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.Control.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer)
       at System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.Control.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.Page.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
       at System.Web.UI.Page.HandleError(Exception e)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
       at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
       at System.Web.UI.Page.LegacyPageAsyncInfo.<CallHandlersPossiblyUnderLock>b__32(Object o)
       at System.Web.HttpContext.InvokeCancellableCallback(WaitCallback callback, Object state)
       at System.Web.UI.Page.LegacyPageAsyncInfo.CallHandlersPossiblyUnderLock(Boolean onPageThread)
       at System.Web.HttpAsyncResult.End()
       at System.Web.UI.Page.LegacyAsyncPageEndProcessRequest(IAsyncResult result)
       at System.Web.UI.Page.AsyncPageEndProcessRequest(IAsyncResult result)
       at ASP.agents_mailoutform_aspx.EndProcessRequest(IAsyncResult ar)
       at System.Web.HttpApplication.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar)
    

    Friday, August 02, 2013 1:56 AM

Answers

  • System.InvalidOperationException: RegisterForEventValidation can only be called during Render();

    This error occurs when you try to render a control to Response.

    The solution is quite simple. You need to notify ASP.Net that not to validate the event by setting the EnableEventValidationflag to FALSE.

    You can set it in the Web.Config in the following way

    <pages enableEventValidation ="false"></pages>

     

    This will apply to all the pages in your website. Else you can also set it in the @Page Directive of the page on which you are experiencing the above error.

    <%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation = "false"


    Caillen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 05, 2013 3:36 AM
  • As it turned out the mailout was hitting a maximum set by the web host. This caused the page to timeout.
    • Marked as answer by FSL_AU Tuesday, August 06, 2013 4:56 AM
    Tuesday, August 06, 2013 4:56 AM

All replies

  • System.InvalidOperationException: RegisterForEventValidation can only be called during Render();

    This error occurs when you try to render a control to Response.

    The solution is quite simple. You need to notify ASP.Net that not to validate the event by setting the EnableEventValidationflag to FALSE.

    You can set it in the Web.Config in the following way

    <pages enableEventValidation ="false"></pages>

     

    This will apply to all the pages in your website. Else you can also set it in the @Page Directive of the page on which you are experiencing the above error.

    <%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation = "false"


    Caillen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 05, 2013 3:36 AM
  • Thank you Caillen. I set EnableEventValidation = "false" for the page then sent a mailout. This time it produced a timeout error after slightly fewer emails than triggered the RegisterForEventValidation error. Could it be that .NET 4.5 doesn't like sending mail using

    smtp.SendAsync

    here is the stack trace

    System.Web.HttpException (0x80004005): Exception of type 'System.Web.HttpException' was thrown. ---> System.Web.HttpException (0x80004005): Request timed out.
       at System.Web.HttpContext.InvokeCancellableCallback(WaitCallback callback, Object state)
       at System.Web.UI.Page.LegacyAsyncPageBeginProcessRequest(HttpContext context, AsyncCallback callback, Object extraData)
       at System.Web.HttpAsyncResult.End()
       at System.Web.UI.Page.LegacyAsyncPageEndProcessRequest(IAsyncResult result)
       at System.Web.UI.Page.AsyncPageEndProcessRequest(IAsyncResult result)
       at ASP.agents_mailoutform_aspx.EndProcessRequest(IAsyncResult ar)
       at System.Web.HttpApplication.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar)
    		

    Monday, August 05, 2013 4:35 AM
  • There is a seperate Forum for ASP, you are a lot more likely to get answers there:

    http://forums.asp.net/


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    Monday, August 05, 2013 7:45 AM
  • Thank you Christopher84. I believe cross-posting is frowned upon. How do I move this thread to the ASP forum?
    Monday, August 05, 2013 8:04 PM
  • Unfortunately we can't move the thread to ASP.NET forum. So please post a new thread there.

    We're sorry for any inconvenience.

    Thanks for your understanding.


    Caillen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 06, 2013 4:21 AM
  • To solve this issue, you may try to specify the timeout in web.config file:

    <httpRuntime executionTimeout="1000" />

    If this still doesn't work, please post in ASP.NET forum for more help from more experts.

    Thanks.


    Caillen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 06, 2013 4:27 AM
  • As it turned out the mailout was hitting a maximum set by the web host. This caused the page to timeout.
    • Marked as answer by FSL_AU Tuesday, August 06, 2013 4:56 AM
    Tuesday, August 06, 2013 4:56 AM