Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

asp.net - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...