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?

5
  • Have you tried mileageText.Text = distance.InnerText ? Commented Nov 28, 2012 at 13:19
  • That doesn't seem to display anything. Commented Nov 28, 2012 at 13:40
  • Did you try element.value? Commented Nov 28, 2012 at 13:47
  • HtmlElement doesn't have a .value member. Commented Nov 28, 2012 at 14:00
  • possible duplicate of Read Javascript variable from Web Browser control Commented Feb 10, 2015 at 16:49

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

6 Comments

No, that wouldn't work as hidden fields don't have InnerHtml - apologies. How about this? mapWebBrowser.Document.GetElementById("distance").GetAttribute("value")
This gives me an error: Object reference not set to an instance of an object.
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.
HTML: <input type="hidden" id="distance" ></input> JavaScript: document.getElementById('distance').value = distance;
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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.