【发布时间】:2019-11-21 09:12:45
【问题描述】:
我的项目中有一个 HTML 页面和一个 .aspx 页面。我在 html 页面中定义了一个 javascript 函数,并试图从 .aspx 页面后面的代码中调用它
我尝试使用 ClientScript.RegisterStartupScript() 和 ScriptManager.RegisterStartupScript() 如下。
HTML 页面 - MainPage.html
<script type="text/javascript">
$(document).ready(function () {
function functionName() {
#content
};
});
</script>
.aspx 页面 - form.aspx.cs
试试 1
protected void Button2_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "MainPage.html", "functionName();", true);
}
试试 2
protected void Button2_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:functionName(); ", true);
}
当单击 aspx 页面的 Button2 时,我试图从 HTML 页面调用函数 functionName()
【问题讨论】:
-
您是否尝试过在
ready函数之外声明functionname? -
@SBFrancies 刚刚尝试过。当我调试应用程序时,它说函数 functionName() 没有在 aspx 页面中定义
-
@HiteshAnshani 在您共享的链接中,javascript 函数在同一页面中被调用,但在我的情况下,在这个 aspx.cs 页面中调用了来自不同 html 页面的函数
-
为什么必须在服务器上调用它,你能不能让它在提交按钮上等待它被点击时作为监听器?
标签: javascript c# html asp.net