Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you Pass an Array from C# to Javascript? 1

Status
Not open for further replies.

sgmurphy

Programmer
Jul 25, 2007
4
US
Hi all,
Not sure what I am doing wrong so hopefully one of you can shed some light on this!
I am creating an ArrayList in C# then passing it to a javascript in a webbrowser.
When trying to read values, I keep getting an "undefined" value for the Array item , i.e. sData[3] in the javascript.
If I do an alert(sData.Count) it does display the correct count for the Array.
Everything up to reading the Array works, the select box gets cleared, etc. But then nothing is passed.
Any help would be appreciated.

C# code:
public class MainForm : Form
{

ArrayList SelectDisplay = new ArrayList();
ArrayList SelectData = new ArrayList();


void PassData_BtnClick(object sender, EventArgs e)
{
FillSelectArrays("Start","none");
FillSelectArrays("One Test","one");
FillSelectArrays("Two Test","two");
FillSelectArrays("Three Test","three");
browser.Document.InvokeScript("FillSelectFromArrays", new object[] { ch.Name, SelectDisplay, SelectData });
}

private void FillSelectArrays(string display, string data)
{
//The Arrays are already defined
SelectDisplay.Add(display);
SelectData.Add(data);
}

private void ClearSelectArrays()
{
SelectDisplay.Clear();
SelectData.Clear();
}

html & javascript:
<html>
<body>

<form>
Test Array: <select name="array_select">
<option value="none"> Values Will Appear Here
</select>
</form>

<script language="javascript" type="text/javascript">

function FillSelectFromArrays(sName, sDisplay, sData)
{
var stot = document.forms[0][sName]
EmptySelect(stot)
with (stot)
{
//Rewrites the text and values
alert("sData[3] = " + sData[3]);
for(i=0; i < sDisplay.Count; i++)
{
options=new Option(sDisplay, sData);
}
options[0].selected=true
}
}

function EmptySelect(sName)
{

var tot = sName.options.length

for (i=0;i {
sName.options=null
}

sName.options.length=0;
}

</script>
</body>
</html>


Thanks!
Sean Murphy
"All things great and small start at the same point, the first step.
 
in the ASP.NET forum look at thread855-1391481 it may help a bit?

Age is a consequence of experience
 
This is a bit of a longshot, but the array created in C# is of type ArrayList right. Could it be that an ArrayList is somehow different in its architecture to a normal array?

I think I would try with a regular array then try passing that over.

(Could be completely wrong as there may not be any differences, but thought id throw in my 2 cents)
 
djfrear has it -- an ArrayList is a complex object specific to .NET, and Javascript just doesn't know how to read it. Use a plain-old array to pass values.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
c# is server side code and javascript is client side code. to pass a c# array to a js array you need to use the ClientScriptManager.RegisterArray(...) to register the array to the client.

If your using MS AJAX you need to use ScriptManager.RegisterArray(). If your not, use Page.ClientScript.RegisterArray() or something like that.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top