如何从aspx.cs或aspx.vb程序启动javascript函数 [英] How do I start a javascript function from a aspx.cs or aspx.vb program

查看:193
本文介绍了如何从aspx.cs或aspx.vb程序启动javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何从aspx.cs或aspx.vb程序启动JavaScript函数?



Default.aspx



How do I start a JavaScript function from a aspx.cs or aspx.vb program?

Default.aspx

<script type="text/javascript">
    function function1()
    {
       ...
    }
</script>





Default.aspx.vb



Default.aspx.vb

Public Class _Default
    Inherits Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
         'Execute some code first
         'Then execute the javascript function
         ClientScript.RegisterClientScriptBlock(Me.GetType(), "JavaScript", "JavaScript:function1(); ", True)
    End Sub
End Class





我尝试了什么:



我在按钮中尝试过OnClientClick或复选框,但我想先运行一些代码,然后执行JavaScript函数。



What I have tried:

I have tried an OnClientClick in a Button or Checkbox but I want to run some code first then execute the JavaScript Function.

推荐答案

你不能从代码隐藏文件中调用js,因为js在浏览器中运行在您的代码隐藏完成呈现页面之后。您可以使用ClientScript.RegisterStartupScript将代码添加到页面中,这样可以在浏览器中加载页面时执行js,但这就是它的全部功能,它实际上并没有执行js然后,由于上述原因,没有什么可以做到这一点。
You can't call js from your code-behind files as js runs in the browser after your code-behind has finished rendering the page. You can add code to the page using ClientScript.RegisterStartupScript which will do it in such a way that the js is executed when the page loads in the browser, but that's all it does, it doesn't actually execute the js there and then, nothing can do that for reasons stated above.


这篇关于如何从aspx.cs或aspx.vb程序启动javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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