Will ClientScript.RegisterStartupScript work only in forms?

限于喜欢 提交于 2019-12-13 06:45:18

问题


I am trying to execute some javascript code in my aspx code behind file. All the examples i have seen on the internet involves code in handler of a button click... But, for my need, i need to use javascript to publish data to clients using APE...

I started by simply having

 ClientScript.RegisterStartupScript(this.GetType(),"alert","alert('hi');",true);

But, it doesn't work. Can someone explain what i have to do to execute javascript code when i dont have a form ?

EDIT: I have a thread that executes every 5s . This thread should execute a javascript code everytime. Do u know how i can do this? thanks


回答1:


Yes, as of ASP.NET 2.0[1] the script will not be added to the page unless it contains a form with the runat attribute set to "server":

<form runat="server">

You don't need to add any controls to the page, so can't you just add the form to your .aspx file? Alternatively, why not just add the call to the script directly to the page? E.g.

<body onload="doStuff();">

[1] http://www.haiders.net/post/Rendering-Client-Script-in-ASPNET-20-Behavior-changes.aspx



来源:https://stackoverflow.com/questions/6602650/will-clientscript-registerstartupscript-work-only-in-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!