Call JavaScript from server side

来源:互联网 发布:mac伴奏降调 编辑:程序博客网 时间:2024/04/30 08:22
It is possible to execute a JavaScript from server-side code. In order to do this, make sure that the code is actually inserted on the page – the easiest way is to put a simple alert() and check if it is fired. Also the controls are rendered on the page before referencing them in your JavaScript function. Please have a look at the following sample code.
protectedvoid RadButton1_Click(objectsender, EventArgs e)
{
stringscriptstring = "alert('JavaScript Executed');";
ScriptManager.RegisterStartupScript(Page, Page.GetType(),"JavaScript", scriptstring,true);
}
 
ASPX:
<head runat="server">    <title></title>    <script type="text/javascript">        function myJavaScript() {            alert("JavaScript Executed");        }     </script></head><body>    <form id="form1" runat="server">    <asp:ScriptManager ID="ScriptManager1" runat="server">    </asp:ScriptManager>    <div>        <telerik:RadButton ID="RadButton1" runat="server" Text="Click" OnClick="RadButton1_Click">        </telerik:RadButton>        <asp:Label ID="Label1" runat="server"></asp:Label>     </div>    </form></body>
c#
protectedvoid RadButton1_Click(objectsender, EventArgs e)
{
Label1.Text ="<script type='text/javascript'>myJavaScript()</script>";
}

原创粉丝点击