1
Answer

Pass an Array from C# to javascript

Sean

Sean

17y
3.4k
1
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[i]=new Option(sDisplay[i], sData[i]);
}
options[0].selected=true
}
}

function EmptySelect(sName)
{

var tot = sName.options.length

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

sName.options.length=0;
}

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

Thanks!
Sean Murphy
"All things great and small start at the same point, the first step."
Answers (1)