Avoiding "Sys.WebForms.PageRequestManagerParserException" when executing jQuery from code-behind
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.
精彩评论