问题
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