开发者

Avoiding "Sys.WebForms.PageRequestManagerParserException" when executing jQuery from code-behind

开发者 https://www.devze.com 2023-04-12 22:24 出处:网络
I am using a Master Page and added jQuery script references in the HEAD section. From the content Form, whenever I t开发者_运维问答ry to execute Response.Write, I frequently get this error:

I am using a Master Page and added jQuery script references in the HEAD section. From the content Form, whenever I t开发者_运维问答ry to execute Response.Write, I frequently get this error:

Sys.WebForms.PageRequestManagerParserException

Though I have found a way to resolve this issue for other sections of my code, I am still struggling when I have to fire jQuery code from code-behind .cs file.

I am using this way:

Response.Write("<script type='text/javascript' language='javascript'>$(function(){$('#dialog:ui-dialog').dialog()};</script>");

Is there any way to bypass this nasty error and use Response.Write?


Response.Write() for outputting JavaScript can work, but it's a bit clumsy and prone to unintended consequences!

The next step up from Response.Write() would be using a Literal control as a placeholder and setting its Text property to your JavaScript string.

The 'better' way to do it is to use the RegisterClientScriptBlock() method (or RegisterStartupScript()) of the ClientScriptManager class. It's simple to use and ensures your script is 'visible' to the containing Page without conflicting with any .NET quirks.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号