Skip to content

Webbrowser and html input text without "post"

Sean

Asked Jul 19, 2007, 2:24 AM EDT1 answer
Share
LinkedInX
I have a webbrowser and a system.windows.forms.button (not an html button).
I want to read the text from input objects, like a basic textbox for a name, in an html document but without it having to be "post"ed.

here's an example of the html doc loaded into the browser

<html>
<body>
<form>
Name: <input name="name">
Address: <input name="address">
</form>
</body>
</html>

Notice there is no submit button on the form.

Someone would fill in their name and a line of their address then click a System.Windows.Forms.Button
Here is my code (which doesn't work as I expected it to)

foreach(HtmlElement elt in browser.Document.Forms)
{
foreach(HtmlElement ch in elt.GetElementsByTagName("INPUT"))
{
MessageBox.Show(ch.Name + " Text = " + ch.InnerText);}
}

I was expecting it would show the text that was typed in but I keep getting a blank return.

Any help would be great!

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

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    For anyone else out there, the code I used was
    C#
    public form1()
    {
    InitializeComponent();

    // Make the form the scripting object
    browser.ObjectForScripting = this;

    // Display the html file file.html
    browser.Navigate(Application.StartupPath + "\\file.html");

    }

    void Button1Click(object sender, EventArgs e)
    {
    foreach(HtmlElement elt in browser.Document.Forms)
    {
    foreach(HtmlElement child in elt.GetElementsByTagName("INPUT"))
    {
    browser.Document.InvokeScript("SendTypedText", new object[] { child.Name });
    }
    }
    }

    public void ShowText(string ibox_name, string ibox_value)
    {
    MessageBox.Show(ibox_name + " = " + ibox_value);
    //Of course now you can do whatever you need to do with the data
    }

    //file.html code
    <html>
    <body>

    <form>
    Box1: <input name="box1">

    Box2: <input name="box2">

    Box3: <input name="box3">


    </form>

    <script language="javascript" type="text/javascript">
    //Send Back Input Values
    function SendTypedText(param)
    {
    var thisForm = document.forms[0];
    window.external.ShowText(param, thisForm[param].value);
    }
    </script>
    </body>
    </html>

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

    0