从服务器端调用Javascript函数

6
在按钮点击时,我调用一个服务器端函数,其中我调用了一个JavaScript函数,如下所示:
Page.ClientScript.RegisterStartupScript(
    this.GetType(), 
    "scriptsKey", 
    "<script type=\"text/JavaScript\" language=\"javascript\">test();</script>");

但是JavaScript函数没有被调用。


1
你在注入脚本的页面上实际上有一个名为 Test() 的方法吗? - James
是的,我的页面中有一个名为test()的方法。 - user2798259
@slugster 我不能从服务器端调用JavaScript函数吗? - user2798259
你能看到这个脚本块在页面源代码中被呈现吗? - Sean Airey
1
@user2798259,你不能从服务器调用JavaScript,而是需要将脚本从服务器注入到客户端页面中。你确定脚本已经在页面中了吗?同时,你是否检查了浏览器中的开发者工具?这可能是JS中的问题。 - James
显示剩余3条评论
3个回答

18
你可以在代码后端像这样调用该函数:

MyForm.aspx.cs

protected void MyButton_Click(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "AnotherFunction();", true);
}

MyForm.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>My Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    function Test() {
        alert("hi");
        $("#ButtonRow").show();
    }
    function AnotherFunction()
    {
        alert("This is another function");
    }
</script>
</head>
<body>
<form id="form2" runat="server">
<table>
    <tr><td>
            <asp:RadioButtonList ID="SearchCategory" runat="server" onchange="Test()"  RepeatDirection="Horizontal"  BorderStyle="Solid">
               <asp:ListItem>Merchant</asp:ListItem>
               <asp:ListItem>Store</asp:ListItem>
               <asp:ListItem>Terminal</asp:ListItem>
            </asp:RadioButtonList>
        </td>
    </tr>
    <tr id="ButtonRow"style="display:none">
         <td>
            <asp:Button ID="MyButton" runat="server" Text="Click Here" OnClick="MyButton_Click" />
        </td>
    </tr>
    </table> 
</form>


添加以上代码后无法正常工作。 - user2798259
我检查了两次。对我来说它很好用。你的代码可能存在其他问题。如果你能展示一下你的代码会更好。 - Md Ashaduzzaman
以下是我在按钮点击时调用的函数。我无法在此处发布代码,因为它太长了。 - user2798259
不,没有Ajax请求。 - user2798259
那我猜你应该提供代码,否则很难帮助。 - Md Ashaduzzaman
显示剩余7条评论

2
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scr", "javascript:test();", true);

这对我有用,谢谢。ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "setValueinEditor();", true); - Ashi

1

这对我有效。希望它也对你有效。

ScriptManager.RegisterStartupScript(this, this.GetType(), "isActive", "Test();", true);

我已编辑您提供的HTML页面。更新后的页面如下:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>My Page</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            function Test() {
                alert("Hello Test!!!!");
                $('#ButtonRow').css("display", "block");
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <table>
            <tr>
                <td>
                    <asp:RadioButtonList ID="SearchCategory" runat="server" RepeatDirection="Horizontal"
                        BorderStyle="Solid">
                        <asp:ListItem>Merchant</asp:ListItem>
                        <asp:ListItem>Store</asp:ListItem>
                        <asp:ListItem>Terminal</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr id="ButtonRow" style="display: none">
                <td>
                    <asp:Button ID="MyButton" runat="server" Text="Click Here" OnClick="MyButton_Click" />
                </td>
            </tr>
        </table>
        </form>
    </body>
    </html>

    <script type="text/javascript">

        $("#<%=SearchCategory.ClientID%> input").change(function () {
            alert("hi");
            $("#ButtonRow").show();
        });
    </script>

在 http://jsfiddle.net/e4kGs/ 上有一个表单,点击按钮后会调用以下服务器端函数。protected void MyButton_Click(object sender, EventArgs e) { Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "Test();", true); } - user2798259
我把“Test”函数移到了“head”标签中,我的代码就可以运行了。另外,在我的代码中,我使用小写字母“t”调用“test”函数。那是个打字错误。我已经改正了。 :) - samar
你能否发布一下你成功运行的代码? - user2798259
嗨,如果我在头部添加修改Test()函数如下: function Test() { alert("你好,测试!!!"); $("#ButtonRow").show(); } - user2798259
ButtonRow没有显示出来 - user2798259

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接