如何从Code Behind中激活Javascript函数 [英] how to fire Javascript Function from Code Behind

查看:56
本文介绍了如何从Code Behind中激活Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在Abc.aspx文件中有一个javascript函数



ex:



Hi I have an javascript Function in Abc.aspx file

ex :

<script type="text/javascript">
     Function ShowChart(){
        //Calling Google API and 
        // Diplaying Chart 
    }    
    </script>







<pre lang="HTML">
<input id="btnPlay" type="button"  runat="server" value="Play"  önclick="initialize()"
                style="width: 80px; height: 30px;" />
<asp:Button ID="loadCh" runat="server" Text="Load Next Value" Width="120px" />





这里上面的Java脚本工作正常当我点击按钮btnPlay并显示图表

但是当我点击按钮loadCh时,会发回一个帖子,我输掉了图表值,所以我需要再次显示图表。

注意按钮LoadCh将从服务器检索数据并填入文本框和所有文件。



之后回帖我希望我的ShowChart()Java脚本被执行!



i试过以下代码后面的代码



here the Above Java Script Works Fine When i click button "btnPlay" and chart is displayed
but when i click button "loadCh" a post back occurs and i loses the chart value so i need to display the chart again.
Note button "LoadCh" will retrieve data from Server and populates in text boxes and all.

after post back i want my ShowChart() Java script to be executed!

i have tried this below Code in code behind

'System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>")
'System.Web.HttpContext.Current.Response.Write("ShowChart();")
'System.Web.HttpContext.Current.Response.Write("</SCRIPT>")
' another Code <pre>



''Page.ClientScript.RegisterClientScriptBlock(Me.GetType(),script,ShowChart();,True)

''Page .ClientScript.RegisterS tartupScript(Me.GetType(),script,ShowChart();,True)





但没有代码解雇了ShowChart函数()



任何人帮我解决方案


''Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "script", "ShowChart();", True)
''Page.ClientScript.RegisterStartupScript(Me.GetType(), "script", "ShowChart();", True)


but none of the Code Fired the function ShowChart()

Any any one help me with a solution

推荐答案

你不能 fire代码隐藏的JavaScript函数,因为代码隐藏在服务器端执行,JavaScript在客户端执行。这不是它的工作原理。如果您了解ASP.NET,您可以看到您可以生成任何可思考的内容,包括带有JavaScript的HTML页面,但它总是在HTTP响应中发生,以响应HTTP请求。换句话说,当JavaScript可以在某个HTML页面上执行时,后面的整个代码已经完成了它的执行。



换句话说,你不能调用JavaScript函数从代码隐藏,但你可以注入一个脚本,在加载页面时调用它。请注意,将调用 onload 属性的脚本以及作为 body 的子元素编写的脚本当页面加载时。



-SA
You cannot "fire" JavaScript functions from code behinds, because code behind is executed on the server side, and JavaScript — on client side. This is not how it works. If you understand ASP.NET, you can see that you can generate any thinkable content, including HTML page with JavaScript, but it always happens in HTTP response, in response to HTTP request. In other words, when JavaScript can be executed on some HTML page, the whole code behind is already completed its execution.

In other words, you cannot call a JavaScript function from code behind, but you can inject a script calling it when a page is loaded. Note that the script of the onload attribute and the script written as the child element of the "body" will be called when the page is loaded.

—SA


使用RegisterStartupScript函数。



ScriptManager.RegisterStartupScript(this,this.GetType(),Showdata,Showdata();,true);



ClientScript.RegisterStartupScript(GetType(),Showdata,Showdata();,true);

:)享受
Use RegisterStartupScript function.

ScriptManager.RegisterStartupScript(this, this.GetType(), "Showdata", "Showdata();", true);

ClientScript.RegisterStartupScript(GetType(), "Showdata", "Showdata();", true);
:) Enjoy


试试这个,


Try this,

ScriptManager.RegisterStartupScript(Page, typeof(Page), "MyScriptShowChart", "ShowChart();", true);


这篇关于如何从Code Behind中激活Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆