Passing arguments to JavaScript function from code-behind

前端 未结 9 2023
孤街浪徒
孤街浪徒 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:21

    If you are interested in processing Javascript on the server, there is a new open source library called Jint that allows you to execute server side Javascript. Basically it is a Javascript interpreter written in C#. I have been testing it and so far it looks quite promising.

    Here's the description from the site:

    Differences with other script engines:

    Jint is different as it doesn't use CodeDomProvider technique which is using compilation under the hood and thus leads to memory leaks as the compiled assemblies can't be unloaded. Moreover, using this technique prevents using dynamically types variables the way JavaScript does, allowing more flexibility in your scripts. On the opposite, Jint embeds it's own parsing logic, and really interprets the scripts. Jint uses the famous ANTLR (http://www.antlr.org) library for this purpose. As it uses Javascript as its language you don't have to learn a new language, it has proven to be very powerful for scripting purposes, and you can use several text editors for syntax checking.

    0 讨论(0)
  • 2020-12-03 18:22

    You can use the Page.ClientScript.RegisterStartupScript method.

    0 讨论(0)
  • 2020-12-03 18:23

    I think you want to execute the javascript serverside and not in the browser after post-back, right?

    That's not possible as far as I know

    If you just want to get it execute after postback, you can do something like this:

    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "xx", "<script>test("+x+","+y+");</script>");
    
    0 讨论(0)
  • 2020-12-03 18:30
    Response.Write("<scrip" + "t>test(" + x + "," + y + ");</script>");
    

    breaking up the script keyword because VStudio / asp.net compiler doesn't like it

    0 讨论(0)
  • 2020-12-03 18:31

    include script manager

    code behind function

    ScriptManager.RegisterStartupScript(this, this.GetType(), "HideConfirmBox", "javascript:HideAAConfirmBox(); ", true);
    
    0 讨论(0)
  • 2020-12-03 18:32
     <head>
        <script type="text/javascript">
    
            function test(x, y) 
            {
                var cc = "";
                for (var i = 0; i < x.length; i++) 
                {
                    cc += x[i]; 
                }
                cc += "\ny: " + y; 
                return cc;
            }
    
        </script>
    
    
    
    </head>
    
    <body>
    
        <form id="form1" runat="server">
    
            <asp:Button ID="Button1" runat="server" Text="Button"   />
    
            <p>
                 <asp:TextBox ID="TextBox1"  Name="TextBox1"  runat="server" AutoPostBack="True"  TextMode="MultiLine"></asp:TextBox>
            </p>
    
    
    
        </form>
    </body>
    
    protected void Page_Load(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(" var y = test({0},{1}) ; ", xStr, yStr);
        script += String.Format("  document.getElementById(\"TextBox1\").value = y  ");
    
        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;
    }
    
    0 讨论(0)
提交回复
热议问题