1

I have a Javascript file that has a global variable which is set to some integer value. I load the Javascript in C# using a WebBrowser control. I need to display the value of the global variable in a WinForms Label.

I have tried putting the global variable in a hidden html field and calling the following C# code:

var distance = mapWebBrowser.Document.GetElementsByTagName("input")["distance"];

        if (distance != null)
            mileageText.Text = Convert.ToString(distance);

But this displays the System.Windows.Forms.HtmlElement in the label whereas I need the actual value stored inside it.

Can anyone help please?

user1696698
  • 219
  • 3
  • 12

1 Answers1

2

Does giving the hidden field an id and calling this work? mapWebBrowser.Document.GetElementById("distance").InnerHtml (where distance is the id)

levelnis
  • 7,665
  • 6
  • 37
  • 61
  • No, that wouldn't work as hidden fields don't have InnerHtml - apologies. How about this? `mapWebBrowser.Document.GetElementById("distance").GetAttribute("value")` – levelnis Nov 28 '12 at 13:54
  • This gives me an error: `Object reference not set to an instance of an object.` – user1696698 Nov 28 '12 at 13:59
  • Could you post the HTML of the hidden field that you're storing the value in? The id might be different, especially if it's been generated from the server side. – levelnis Nov 28 '12 at 14:03
  • HTML: `` JavaScript: `document.getElementById('distance').value = distance;` – user1696698 Nov 28 '12 at 14:13
  • It may not make a difference but could you make the input self-closing? Also, could the server-side code be trying to access the value before the javascript has written it to the hidden field? – levelnis Nov 28 '12 at 14:18
  • i think that might be what is happening. I will try using an element where I can set the inner html and use the .InnerHtml method you suggested. Thanks levelnis. – user1696698 Nov 28 '12 at 14:30