Retrieve element attribute value when not initially defined in document text

434 Views Asked by At

Say we have a WebKitBrowser control and we set the DocumentText as such...

wkb.DocumentText = @"
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
<html>
<head></head>
<body>
<form>
<input type='text' id='test' name='smith'><br>
</form>
</body>
</html>
";

If I was to then fetch the user input in that input field via C#, what is the proper method?

If I predefine the value attribute in the HTML, like...

<input type='text' id='test' name='smith' value='freedom'>

Then MessageBox.Show(wkb.Document.GetElementById("test").getAttribute("value")); accurately displays "freedom". However, if value is not defined, the result is always blank (I'm assuming it's null and not just empty).

This makes sense to me but I would be fetching user input, not predefined values. I suppose one could save the entire document to a string and parse it but it was much more simple in the standard IE browser control with the element.value route. What are my delicious options?

2

There are 2 best solutions below

0
viperguynaz On BEST ANSWER

You can't read form values in C# client side. You have to post (AJAX or full postback) the form back to read the values in C#. If you want to see if a value has changed, you can use jQuery's .on('blur' ... to capture the changed value and do an AJAX post back to the server.

0
Ray Alex On

I also wanted to add, this method works, sort of going off of viperguynaz's proper answer.

Essentially you create a navigating method for the webkit browser control, then point the webkit browser control do it in the events section. You perform a functionally useless form submission on the HTML side and then parse the parameters for the user input and off you go, to whatever fantasy awaits you.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using WebKit;

namespace test_run
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            wkb.DocumentText = @"
                <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
                <html>
                <head></head>
                <body>
                <form method='get'>
                <input type='text' name='pike'><br>
                <input type='submit' value='Submit'>
                </form>
                </body>
                </html>";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void wkb_navigating(object sender, WebKitBrowserNavigatingEventArgs e)
        {
            string param1 = HttpUtility.ParseQueryString(e.Url.ToString()).Get(0);
            MessageBox.Show(param1.ToString());


        }
    }
}