Passing arguments to JavaScript function from code-behind

前端 未结 9 2024
孤街浪徒
孤街浪徒 2020-12-03 17:54

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


    

        
相关标签:
9条回答
  • 2020-12-03 18:33

    Some other things I found out:

    You can't directly pass in an array like:

    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "xx",   
    "<script>test("+x+","+y+");</script>");
    

    because that calls the ToString() methods of x and y, which returns "System.Int32[]", and obviously Javascript can't use that. I had to pass in the arrays as strings, like "[1,2,3,4,5]", so I wrote a helper method to do the conversion.

    Also, there is a difference between this.Page.ClientScript.RegisterStartupScript() and this.Page.ClientScript.RegisterClientScriptBlock() - the former places the script at the bottom of the page, which I need in order to be able to access the controls (like with document.getElementByID). RegisterClientScriptBlock() is executed before the tags are rendered, so I actually get a Javascript error if I use that method.

    http://www.wrox.com/WileyCDA/Section/Manipulating-ASP-NET-Pages-and-Server-Controls-with-JavaScript.id-310803.html covers the difference between the two pretty well.

    Here's the complete example I came up with:

    // code behind
    protected void Button1_Click(object sender, EventArgs e)
    {
        int[] x = new int[] { 1, 2, 3, 4, 5 };
        int[] y = new int[] { 1, 2, 3, 4, 5 };
    
        string xStr = getArrayString(x); // converts {1,2,3,4,5} to [1,2,3,4,5]
        string yStr = getArrayString(y);
    
        string script = String.Format("test({0},{1})", xStr, yStr);
        this.Page.ClientScript.RegisterStartupScript(this.GetType(),
        "testFunction", script, true);
        //this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
        //"testFunction", script, true); // different result
    }
    private string getArrayString(int[] array)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.Length; i++)
        {
            sb.Append(array[i] + ",");
        }
        string arrayStr = string.Format("[{0}]", sb.ToString().TrimEnd(','));
        return arrayStr;
    }
    
    //aspx page
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
        <script type="text/javascript">
        function test(x, y)
        {
            var text1 = document.getElementById("text1")
            for(var i = 0; i<x.length; i++)
            {
                text1.innerText += x[i]; // prints 12345
            }
            text1.innerText += "\ny: " + y; // prints y: 1,2,3,4,5
    
        }
    
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button"
             onclick="Button1_Click" />
        </div>
        <div id ="text1"> 
        </div>
        </form>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-03 18:36

    Look at the ScriptManager.RegisterStartupScript method if you're using a ScriptManager or any Ajax controls/asynchronous postbacks.

    Edit:

    Actually, the function you want is probably ScriptManager.RegisterClientScriptBlock

    0 讨论(0)
  • 2020-12-03 18:39
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Call java script function on Code behind</title>
        <script  type="text/javascript">
        function abc()
        {
            var a=20;
            var b=30;
            alert("you enter"+a+":"+b);
        }
        </script>
    </head>
    

    cs code

    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox2.Attributes.Add("onkeypress", "return abc();");
    }
    

    try this

    0 讨论(0)
提交回复
热议问题