Help Passing ArrayList form C# to Javascript
-
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:
Test Array: Values Will Appear Here
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<tot;i++) { sName.options[i]=null } sName.options.length=0; }
Thanks! Sean Murphy "All things great and small start at the same point, the first step."
-
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:
Test Array: Values Will Appear Here
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<tot;i++) { sName.options[i]=null } sName.options.length=0; }
Thanks! Sean Murphy "All things great and small start at the same point, the first step."
-
have you tried using an old-school array instead of ArrayList? (i.e. yourArrayList.ToArray()) javascript is probably not capable of handling non-basic .NET types ;)
Definitely a great idea and one I hadn't thought of, but unfortunately it didn't work. I even tried creating it as an object[] then putting that into a collection. Arg :(
Thanks! Sean Murphy "All things great and small start at the same point, the first step."