如何从后面的代码中调用用aspx编写的javascript? [英] How to call javascript written in aspx from code behind?
本文介绍了如何从后面的代码中调用用aspx编写的javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我必须从服务器端调用javascript的情况
有可能的话请建议
Here is the scenario i have to call javascript from server side
is it possible then please suggest
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function test(x, y)
{
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click"/>
</div>
</form>
</body>
</html>
protected void Button1_Click(object sender, EventArgs e)
{
// do stuff (really going to a database to fill x and y)
// call javascript function as test(x,y);
}
推荐答案
使用Page.ClientScript.RegisterStartupScript
.
UsingPage.ClientScript.RegisterStartupScript
.
string xValue = "SomethingX";
string yValue = "SomethingY";
Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript",
"javascript:test("+ xValue + ", "+ yValue +")", true);
这篇关于如何从后面的代码中调用用aspx编写的javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文