22

I have a webmethod which will be called from jquery ajax:

[WebMethod]
public string TestMethod(string param1, string param2)
{
    StringBuilder b = new StringBuilder();
    HtmlTextWriter h = new HtmlTextWriter(new StringWriter(b));
    this.LoadControl("~/Pages/Controls/Listing.ascx").RenderControl(h);
    string controlAsString = b.ToString();
    return controlAsString;
}

(it's a non-static method and we are able to hit it. That's not an issue)

When the loadControl() method is executed, I get an error saying: RegisterForEventValidation can only be called during Render.

I have already included EnableEventValidation="false" for the current aspx, disabled viewstate also. but still i get the same error. Any ideas on this?

Amit
  • 25,106
  • 25
  • 75
  • 116

3 Answers3

62

Solution is to disable the Page's Event Validation as

<%@ Page ............ EnableEventValidation="false" %>

and Override VerifyRenderingInServerForm by adding following method in your C# code behind

public override void VerifyRenderingInServerForm(Control control)
{
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
       server control at run time. */
}

Refer the Below Link

http://www.codeproject.com/Questions/45450/RegisterForEventValidation-can-only-be-called-duri

Syed Mohamed
  • 1,339
  • 1
  • 15
  • 23
  • Thank you! This was exactly the answer I needed for my question: http://stackoverflow.com/q/24457003/363892. – Justin Holdsclaw Jun 27 '14 at 19:57
  • could you help on this for same.... http://stackoverflow.com/questions/40629644/how-to-export-excel-without-enableeventvalidation-false-because-of-lock-issues – Sukhvindra Singh Dec 06 '16 at 10:43
  • Doesn't work. I get the error: VerifyRenderingInServerForm(Control): no suitable method found to override – Janco de Vries Nov 22 '18 at 12:37
  • You current class where your writting this should be proper class inheritted from Page base class @JancodeVries : Try as stated in "Solution 1" of this https://www.codeproject.com/Questions/321382/about-VerifyRenderingInServerForm-event – Syed Mohamed Nov 23 '18 at 04:37
  • You'll see this when someone uses async void in page events despite that not really being supported. Resolve by using one of the many AsyncUtils to run any async code in a sync handler or use the RegisterAsyncTask(new PageAsyncTask()) method supported by ASP.NET webforms. – James White Feb 22 '21 at 19:25
12

As per Brian's second link, without hopping further, just do this:

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
Html32TextWriter hw = new Html32TextWriter(sw);

Page page = new Page();
HtmlForm f = new HtmlForm();
page.Controls.Add(f);
f.Controls.Add(ctrl);
// ctrl.RenderControl(hw); 
// above line will raise "RegisterForEventValidation can only be called during Render();"
HttpContext.Current.Server.Execute(page, sw, true);
Response.Write(sb); 
KMX
  • 2,631
  • 1
  • 23
  • 28
  • could you help on this for same.... http://stackoverflow.com/questions/40629644/how-to-export-excel-without-enableeventvalidation-false-because-of-lock-issues – Sukhvindra Singh Dec 06 '16 at 10:43
  • 1
    You saved my day ! – Mlle 116 Aug 28 '17 at 10:22
  • @SukhvindraSingh sorry for late reply. just go check my answer on that question. thanks. – KMX Aug 28 '17 at 15:32
  • Can anyone explain this one to those of us finding this now? Declaration of **ctrl** is missing, for starters. (It mentions an unknown Brian, so maybe it refers to a now-deleted answer or comment that explained what this is doing...?) – MGOwen May 17 '18 at 09:46
  • @MGOwen the "ctrl" is short of "control" and it is a var that you declare anywhere to hold a ref to Control object.Since we are talking about adding control in the page controls so declare the control anywhere in the code before f.Control.Add(ctrl) and you are done! – KMX Oct 23 '18 at 12:52
0

You need to notify ASP.Net that not to validate the event by setting the EnableEventValidation flag to false.

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

<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation = "false"
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 14 '22 at 08:36