Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegisterForEventValidation can only be called during Render

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?

like image 942
Amit Avatar asked Aug 29 '11 10:08

Amit


People also ask

How do you solve Registerforeventvalidation can only be called during Render ()?

The solution is quite simple you need to notify ASP.Net that not to validate the event by setting the EnableEventValidation flag to FALSE. 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.

What is the use of EnableEventValidation in asp net?

When the EnableEventValidation property is set to true , ASP.NET validates that a control event originated from the user interface that was rendered by that control. A control registers its events during rendering and then validates the events during postback or callback handling.


2 Answers

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

like image 129
Syed Mohamed Avatar answered Nov 08 '22 23:11

Syed Mohamed


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); 
like image 43
KMX Avatar answered Nov 09 '22 01:11

KMX