如何从位于另一个asp.net页面的asp.net c#页面调用javascript函数?
问题描述:
我想从第一页的代码后面的2个asp.net页面调用第二个asp.net页面中的javascript函数。任何例子?
I have 2 asp.net pages I want from code behind of the first page to call a javascript function in the second asp.net page. any examples?
答
你制作第一个外部javascript文件。并在页面上调用此文件。
是这样的:
< script type =text / javascriptsrc =../ page / file.js>< / script>
Ex。
代码背后
You make first External javascript file. And call this file on page .
like this:
<script type="text/javascript" src="../page/file.js"></script>
Ex.
Code Behind
protected void DoSomethingButton_Click(object sender, EventArgs e)
{ScriptManager.RegisterStartupScript(this, Page.GetType(), "key", "MyFunc()", true);
}
据我所知,这是正常语法无法实现的,但你可以做一件事。
1.为Javascript代码创建单独的js文件
2.在两个aspx文件中引用该文件
As far as I know, this is not possible with normal syntax but you can do one thing.
1. Create a separate js file for Javascript code
2. Give reference to that file in both the aspx files
<script src="yourscript.js"></script>
3.现在您可以使用js中包含的所有功能文件
从后面的代码调用Javascript函数:
3. Now you can all the functions contained in the js file
To call Javascript function fro code behind :
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "YourJSFunction();", true);
希望,它有助于:)
Hopefully, it helps :)