How tp pass array to javascript function
can anybody plz tell me hw to pass an array from cs file to javascript function in source file in c#
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 3, 2011, 8:17 PM EST
I don't know if there's such method out-there.. to pass variables value to javascript.
well i have another idea to achieve this.. add an hidden field to your page assign the value of the array by adding all values comma separated. and then retreive the value from this hidden field through javascript and use Split() function to convert it back to an array.
for e.g.,
string[] arr={asb,aba,aas};
use loop to make this array as comma separated string. then assign that string as hidden field value.
string arrstring="asb,aba,aas";
HiddenField1.value=arrstring;
now in your aspx page retrieve the value using javascript.
string comsep=document.getElementById('<%HiddenField.ClientID%>');
var arr=comsep.Split(',');
This solution is only for 1 dimensional array.
Hope this helps you.
Please don't forget to mark "Do you like this answer" if it helps you.
Thanks.
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Mar 3, 2011, 8:21 PM EST
you can try this.
// 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"> Untitled Pagetitle> script type="text/javascript"> function test(x, y) { var text1 = document.getElementById("text1") for(var i = 0; ix.length; i++) { text1.innerText += x[i]; // prints 12345 } text1.innerText += "\ny: " + y; // prints y: 1,2,3,4,5 } script> head> form id="form1" runat="server"> asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> div> div id ="text1"> div> form> body> html>Soft CornerRank #1,221 · 1.5K reputationPosted Mar 3, 2011, 8:41 PM EST
Take This Example of How can u pass ArrayList To JavaScript function
swati agrawal18 reputationPosted Mar 3, 2011, 9:31 PM EST
so can u suggest hw to call dis function in pageload event of cs page.actually m calling it like dis bt its nt doing anything.
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Message", "", false);
Sam HobbsRank #107 · 20.5K reputationPosted Mar 3, 2011, 9:43 PM EST
Suthish NairRank #92 · 22.8K reputationPosted Mar 3, 2011, 9:46 PM EST
swati agrawal18 reputationPosted Mar 3, 2011, 10:00 PM EST
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 3, 2011, 11:08 PM EST
your question and what you have asked by giving example both are vice versa of eachother.. so you should be clear to your problem first, otherwise you'll get confuse and at last will find no exact solution..
Identifying the problem will ensure the way to the solution and reduce your time and effort.
:)
Eric Frazin2 reputationPosted Mar 4, 2011, 5:16 AM EST
I may be wrong about this does "this.Page.ClientScript.RegisterStartupScript" handle this already?
Here is the code to finish the process.
//Start Code:
private void SaveToFile(string FilePath) {
System.Text.StringBuilder Build = new System.Text.StringBuilder();
System.IO.TextWriter Save = new System.IO.StreamWriter(FilePath);
Save.Write(Build.ToString());
Save.Flush();
Save.Close();
}
private void ButtonSave_Click(object sender, EventArgs e) {
try
{
SaveToFile("JSTEST.html");
}
catch
{
}
}
//End Code!!!!!
I am primarily a VB programmer, fairly new to C# so please correct any mistakes you may find, as this code is untested. - BrÜ
Raja KrishnamurthyRank #3,615 · 403 reputationPosted Mar 4, 2011, 6:09 AM EST
so can u suggest hw to call dis function in pageload event of cs page.actually m calling it like dis bt its nt doing anything.
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Message", "", false);
I see two problems with the code.
1. For the Slide() function to work you got to use RegisterStartupScriptBlock (renders the JS at the end of the page) rather than using ClientScriptBlock;
2. You cannot just use var path = document.getElementById("HiddenField1.ClientID").value; if you want to use HiddenField1's client ID. Your statement checks for the exact ID HiddenField1.ClientID which I think is not what you want. Its been a while since I worked in ASP .Net web forms but I think you got to use document.getElementById("<%=HiddenField1.ClientID%>").value;
Finally I dont know what your function slide() does??
Hope this helps.
Happy Progamming!!!
Regards,
Raja
Sam HobbsRank #107 · 20.5K reputationPosted Mar 4, 2011, 8:00 AM EST
Suthish NairRank #92 · 22.8K reputationPosted Mar 5, 2011, 2:25 AM EST
Suthish NairRank #92 · 22.8K reputationPosted Mar 7, 2011, 9:17 AM EST