Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help Passing ArrayList form C# to Javascript

Help Passing ArrayList form C# to Javascript

Scheduled Pinned Locked Moved C#
csharpjavascripthtmldata-structureshelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sean G Murphy
    wrote on last edited by
    #1

    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."

    J 1 Reply Last reply
    0
    • S Sean G Murphy

      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."

      J Offline
      J Offline
      jkersch
      wrote on last edited by
      #2

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

      S 1 Reply Last reply
      0
      • J jkersch

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

        S Offline
        S Offline
        Sean G Murphy
        wrote on last edited by
        #3

        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."

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups