RegisterForEventValidation can only be called during Render

前端 未结 2 1784
野的像风
野的像风 2020-12-03 10:49

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

[WebMethod]
public string TestMethod(string param1, string param2)
{
    StringBuilder b = new Stri         


        
相关标签:
2条回答
  • 2020-12-03 11:05

    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); 
    
    0 讨论(0)
  • 2020-12-03 11:06

    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

    0 讨论(0)
提交回复
热议问题