Populating Javascript array from vb.net

  • Thread starter Thread starter MW
  • Start date Start date
M

MW

How can I poplute a javascript array from vb.net? I would prefer to do this
from codebehind.

Michael
 
Create a String that represents the code and register it as a client script

dim str as String = "<script>var i=new
Array();i[0]=234;i[1]=543;i[2]=598;</script>"
page.RegisterClientScriptBlock(""myScript",str)

(this method does not show up in intellisense, but you can see it if you
look at the page class through the object browser)
 
Hi,
you can add the following code in your page to create an array also
you can edit this code to add your on logic for assigning the values in to
the array.

Private Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
' Put user code to initialize the page here
Me.RegisterStartupScript("SetFocusScript",
PopulateJavaScriptArray())
End Sub


Private Function PopulateJavaScriptArray() As String
' Declaring the Number of Items in the Array
Dim noOfItems As Integer = 10
Dim myJavaScript As StringBuilder = New StringBuilder()
myJavaScript.Append("<script language='Javascript'>")
myJavaScript.Append(" var myArrayInJavaScript = new Array("+
noOfItems +");")
myJavaScript.Append(" myArrayInJavaScript[5] ='Hello
There...!!';")
myJavaScript.Append(" alert(myArrayInJavaScript[5]);")
myJavaScript.Append("</script>")
Return myJavaScript.ToString()
End Function

HTH
Regards
Ashish M Bhonkiya
 
check this code

String scriptString = "<script language=JavaScript> function doClick() {";
scriptString += "for(var index=0;index < myArray.length;index++)";
scriptString += " myArray[index].show(); } <";
scriptString += "/" + "script>";

RegisterStartupScript("arrayScript", scriptString);
RegisterArrayDeclaration("myArray", "new obj('x'),new obj('y'),new
obj('z')");

you can declare your array using your values.

Av.
 
Back
Top