Since JS is client-side and ASP.NET is server side, the only way to do this, AFAIK, is by outputting the contents of your VB Array to the page.
Here is a bit of an example.
First in your .aspx page:
<Script Language="JavaScript">
<asp:Label id="array" runat="server"/>
</script>
Then in your code behind:
array.Text="var jsArray=new Array("+vbArray.Count+");";
for(int i=0;i<vbArray.Count;i++){
array.Text+="jsArray["+i.ToString()+"]='"+vbArray[i]+"';";
}
I would imagine you would need to build a string which represents a javascript array. Loop through your asp array and create a string which represents the array structure below. Maybe there is a more elegant solution but I have not seen one.
Dim options As string = ['one','two','three','four']
Me.drpOperator1.Attributes.Add("onChange", "Javascript:betweenPanelOption(" + options+ ");")
Hi all, I'm having trouble passing a multidimensional array from my code-behind page in C#.
I'm doing this in a popup window, listing datalist for the user to choose supplier, when they click the add button, I need to send back an multidimensional array to the parent window. Then in the parent window I have a JavaScript function that takes the array
and fills a DropDownList.
Any suggestions to how to solve the buildin of the array in the pop up window?
benyang2008
Member
90 Points
18 Posts
How to pass array variable from asp.net to javascript?
Aug 11, 2005 04:05 PM|LINK
Has anyboby ever done that before?
I don't know how to access a asp.net array in a javascript function. Can anybody help?
yonah
Member
590 Points
118 Posts
Re: How to pass array variable from asp.net to javascript?
Aug 11, 2005 04:47 PM|LINK
Since JS is client-side and ASP.NET is server side, the only way to do this, AFAIK, is by outputting the contents of your VB Array to the page.
Here is a bit of an example.
First in your .aspx page:
<Script Language="JavaScript">
<asp:Label id="array" runat="server"/>
</script>
Then in your code behind:
array.Text="var jsArray=new Array("+vbArray.Count+");";
for(int i=0;i<vbArray.Count;i++){
array.Text+="jsArray["+i.ToString()+"]='"+vbArray[i]+"';";
}
theeggman
Member
115 Points
23 Posts
Re: How to pass array variable from asp.net to javascript?
Aug 11, 2005 04:49 PM|LINK
Dim options As string = ['one','two','three','four']
Me.drpOperator1.Attributes.Add("onChange", "Javascript:betweenPanelOption(" + options+ ");")
A1ien51
All-Star
29935 Points
5821 Posts
Re: How to pass array variable from asp.net to javascript?
Aug 11, 2005 04:58 PM|LINK
Eric
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: How to pass array variable from asp.net to javascript?
Aug 11, 2005 05:47 PM|LINK
Teemu Keiski
Finland, EU
benyang2008
Member
90 Points
18 Posts
Re: How to pass array variable from asp.net to javascript?
Aug 11, 2005 06:45 PM|LINK
Resolved
benyang2008
Member
90 Points
18 Posts
Re: How to pass array variable from asp.net to javascript?
Aug 15, 2005 06:00 PM|LINK
Resolved
maqster
Member
55 Points
11 Posts
Re: How to pass array variable from asp.net to javascript?
Aug 19, 2005 09:11 AM|LINK
I'm doing this in a popup window, listing datalist for the user to choose supplier, when they click the add button, I need to send back an multidimensional array to the parent window. Then in the parent window I have a JavaScript function that takes the array and fills a DropDownList.
Any suggestions to how to solve the buildin of the array in the pop up window?
Her is a code fragment.
string
extContacts = String.Empty; string supplierId = ((DataRowView)e.Item.DataItem).Row["SupplierId"].ToString(); string supplierName = ((DataRowView)e.Item.DataItem).Row["SupplierName"].ToString().Replace("'", "'"); using(ContactServices cs = new ContactServices()){
foreach(DataRow r in cs.GetExtContactListBySupplierId(new Guid(supplierId)).Rows){
DataRow pRow = (DataRow)cs.GetPerson(
new Guid(r["PersonId"].ToString())).Rows[0]; if(pRow != null){
extContacts = extContacts + "[" + pRow["PersonId"].ToString() +", " + pRow["FirstName"] + "]" ;
}
}
}
Button matchedSupplierAddButton = (Button)e.Item.FindControl("matchedSupplierAddButton");
matchedSupplierAddButton.Attributes.Add("onClick", "window.opener.SetSupplier('" +
this.form + "','" + this.elementId1 + "','" + this.elementId2 + "', '" + this.elementId3 + "', '"+ supplierId +"','" + supplierName + "', '[" + extContacts + "]' ," + postBack + ");");