Populating Javascript array from vb.net

M

MW

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

Michael
 
D

David Jessee

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)
 
A

Ashish M Bhonkiya

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
 
A

avnrao

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top