asp.net jquery rendercontrol

asp.net - RegisterForEventValidation solo se puede llamar durante el Render



jquery rendercontrol (2)

La solución es deshabilitar la validación de eventos de la página como

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

y anule a VerifyRenderingInServerForm agregando el siguiente método en su código C # detrás

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

Consulte el siguiente enlace

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

Tengo un método web que se llamará desde 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; }

(Es un método no estático y podemos golpearlo. Eso no es un problema)

Cuando se ejecuta el método loadControl (), aparece un error que dice: RegisterForEventValidation solo se puede llamar durante el Render.

Ya he incluido EnableEventValidation = "false" para el aspx actual, también se deshabilitó viewstate. Pero todavía me sale el mismo error. ¿Alguna idea sobre esto?


Según el segundo enlace de Brian, sin saltar más, simplemente haga esto:

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);