jQuery RegisterForEventValidation 只能在 Render 期间调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7228718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:34:06  来源:igfitidea点击:

RegisterForEventValidation can only be called during Render

asp.netjqueryrendercontrol

提问by Amit

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

我有一个将从 jquery ajax 调用的 webmethod:

[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.

执行 loadControl() 方法时,我收到一条错误消息:RegisterForEventValidation 只能在渲染期间调用。

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

我已经为当前的 aspx 包含了 EnableEventValidation="false",也禁用了视图状态。但我仍然遇到同样的错误。对此有何想法?

回答by Syed Mohamed

Solution is to disable the Page's Event Validation as

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

解决方案是禁用页面的事件验证作为

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

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

并通过在后面的 C# 代码中添加以下方法来覆盖 VerifyRenderingInServerForm

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

请参阅以下链接

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

回答by KMX

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