简体   繁体   English

RegisterForEventValidation 只能在 Render 期间调用

[英]RegisterForEventValidation can only be called during Render

I have a webmethod which will be called from jquery ajax:我有一个网络方法,将从 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.当执行 loadControl() 方法时,我收到一条错误消息:RegisterForEventValidation can only be called during Render。

I have already included EnableEventValidation="false" for the current aspx, disabled viewstate also.我已经为当前的 aspx 添加了 EnableEventValidation="false",还禁用了视图状态。 but still i get the same error.但我仍然得到同样的错误。 Any ideas on this?对此有什么想法吗?

Solution is to disable the Page's Event Validation as 解决方案是禁用Page的事件验证

<%@ 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

As per Brian's second link, without hopping further, just do this: 根据Brian的第二个链接,没有进一步跳跃,只需这样做:

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

You need to notify ASP.Net that not to validate the event by setting the EnableEventValidation flag to false .您需要通过将EnableEventValidation标志设置为false来通知 ASP.Net 不要验证该事件。

You can set it in the Web.Config in the following way您可以通过以下方式在 Web.Config 中进行设置

<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation = "false"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 只能在render()期间调用RegisterForEventValidation,并且由于代码已优化,因此无法评估表达式 - RegisterForEventValidation can only be called during render() and Unable to evaluate expression because the code is optimized "SetFocus 只能在 PreRender 之前和期间调用" - SetFocus can only be called before and during PreRender 如何使用ClientScriptManager.RegisterForEventValidation注册控件 - How can I register a control with ClientScriptManager.RegisterForEventValidation ClientScript.RegisterForEventValidation和__doPostBack() - ClientScript.RegisterForEventValidation and __doPostBack() ASP.NET中是否存在可以在运行时呈现HTML的控件? - Is there such a control in ASP.NET that can render HTML during runtime? 应该如何正确使用RegisterForEventValidation - how should RegisterForEventValidation be used correctly ASP.NET生命周期中的哪个方法仅被调用一次? 回发期间不触发 - which method in ASP.NET life-cycle is only called once? It is not fired during postback 如何对转发器使用ClientScriptManager.RegisterForEventValidation - How To Use ClientScriptManager.RegisterForEventValidation For Repeater 正确使用Page.ClientScript.RegisterForEventValidation - Correct usage for Page.ClientScript.RegisterForEventValidation 事件处理程序只能在IHttpModule初始化期间绑定到HttpApplication事件 - Event handlers can only be bound to HttpApplication events during IHttpModule initialization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM