0

Can anyone please tell me how to call javascript function with string arrays as argument and how to use this arrays in the called function. Here is my code:

C# Code

for (int i = 0; i < count; i++)
{
    array1[i] = dt.Rows[i]["FieldName"].ToString();
    array2[i] = dt.Rows[i]["FieldValue"].ToString();
}
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"Javascript", "<script type='text/javascript'>myFunction('" + count + "','" + array1 + "','" + array2 + "');</script>");

JavaScript

function myFunction(cnt, arr1, arr2)
{
    for (i = 0; i < cnt; i++)
    {
        alert("fname" + arr1[i] + " :fvalue " + arr2[i] + " :count:" + cnt);
    }
}

any syntax error in passing array variables.

8
  • the arr1 and arr2 is string in your javascript function. so if you do arr1[i], you actually will get characters I think. For what you want, I think you can split the arr1 and arr2 and use it as an array accordingly. Commented May 4, 2015 at 11:46
  • @PaulFitzgerald Regard the C#-Code. In cnt in the JavaScript-Code is the count-variable from C#-Code stored. So it already is the count, not the array itself. Commented May 4, 2015 at 11:48
  • @PaulFitzgerald is right.. just add(.length) after cnt Commented May 4, 2015 at 11:49
  • @PatrikEckebrecht I have no idea about C# tbh, was just the first thing I noticed when looking at the JS Commented May 4, 2015 at 11:49
  • 1
    if cnt is a number, which now makes sense, if OP puts var before i at the start it should work, however based on the answer below, it looks as if my interpretation of the question is way off Commented May 4, 2015 at 11:56

2 Answers 2

2

You could leverage javascript's apply method here and use arguments variable and treat it as the source of your array thereby avoiding messy 'args' on your method. You can then pass a string like:

 //After ensuring all elements in array1 are wrapped in ' characters or are appropriate as arguments as-is
 "myFunction.apply(this, [" + array1.join(",") + "])".

This way "myFunction" will handle ANY number of arguments or (using the javascript arguments object) array of any size. No need to pass 'count' variable. myFunction will could then be written as:

 function myFunction() {
     for (var j = 0, len = arguments.length; j < len; j++) {
         var a = arguments[j];
         //do something with a
     } 
 }
Sign up to request clarification or add additional context in comments.

Comments

1

How to call a javascript function with string arrays

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"Javascript", "<script type='text/javascript'>myFunction('" + count + "','[" +string.Join(",", array1) + "]','[" + string.Join(",", array2)+ "]');</script>");

nb : you can save the scripts tag via :

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"Javascript", "myFunction('" + count + "','[" +string.Join(",", array1) + "]','[" + string.Join(",", array2)+ "]');",true);

Also , you're adding the same key again and again ( "Javascript") :

try adding guid :

"Javascript"+Guid.NewGuid().ToString("n")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.