我想弄清楚为什么Page.ClientScript.RegisterClientScriptBlock不工作,而ScriptManager.RegisterClientScriptBlock 却工作
我的代码使用以下代码:
this.Page.ClientScript.RegisterClientScriptBlock(GetType(), "headerComment", "<script>NOT WORKING</script>", false);在过去,它曾经起作用
所以我试着找一个解决方案,但是我发现母版页不包含<form id="form1" runat="server">。
当我添加form标记this.Page.ClientScript.RegisterClientScriptBlock不起作用时,我试着使用ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "headerComment", "<script>WORKING</script>", false);,它起了作用。
我更喜欢使用Page.ClientScript.RegisterClientScriptBlock而不是ScriptManager.RegisterClientScriptBlock,这样我就不需要修改现有的工作代码了。
我没有发现如果这两者之间有什么区别。以及ScriptManager.RegisterClientScriptBlock的更改将导致我的应用程序中的这种更改。
有什么理由:
this.Page.ClientScript.RegisterClientScriptBlock(GetType(), "headerComment", "<script>NOT WORKING using 'Page.ClientScript.RegisterClientScriptBlock'</script>", false);不管用吗?
发布于 2017-08-20 13:37:44
对我有用的解决方案是用带有form属性的runat="server"标记包装母版页的主体内容:
<body>
<form id="form1" runat="server">
...
...
</form>
</body>然后,从在母版页内运行的任何后台代码中,下面的代码可以工作
Page.ClientScript.RegisterClientScriptBlock(GetType(), "scriptId", "<script>console.log('working');</script>", false);https://stackoverflow.com/questions/45780379
复制相似问题