从C-背后$ C $将参数传递给JavaScript函数函数、参数、JavaScript

2023-09-02 11:52:30 作者:战皆罪

我想从一个aspx控制调用javascript函数。举例来说,假设我有:

I would like to call a javascript function from an aspx control. For instance, suppose I had:

<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>

和背后的code:

protected void Button1_Click(object sender, EventArgs e)
{
    // do stuff (really going to a database to fill x and y)
    int[] x = new int[] { 1, 2, 3, 4, 5 };
    int[] y = new int[] { 1, 2, 3, 4, 5 };

    // call javascript function as test(x,y);
}

有没有办法做到这一点?

Is there a way to do it?

推荐答案

您可以使用Page.ClientScript.RegisterStartupScript方法。