views:

1412

answers:

5

Is there a way to uncheck/check a checkbox within a webpage that is loaded within a webbrowser control? Thank you.

Update: (This is what I originally tried with no luck)

HtmlDocument rememberme = this.webBrowser1.Document;
rememberme.GetElementById("remBox").SetAttribute("checked", "false");
A: 

In javascript you should be able to use the clientId to set .checked = false.

Myles
What does that have to do with the webbrowser control?
Telos
+2  A: 

You can use:

webBrowser.Document.InvokeScript

see:

InvokeScript

This way you can call JS function that will do what you want to the page.

Another way is to use mshtml API, like this: ( ( HTMLInputElement )this.webBrowser1.Document.GetElementById( "test" ).DomElement ).@checked = false;

Majkel
What would be a proper way to use JavaScript to uncheck a single checkbox?
Nate Shoffner
It depends - if you know checkbox's ID, then just use `document.getElementById( 'yourid' ).checked = false;`But if it is dynamic (like in ASP.NET in control) than you would have to use another way - locate it by class name, traverse DOM tree or something.
Majkel
A: 

http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.invokescript.aspx you need to analyze the page structure to find out how to locate the checkbox and use Myles's method to check/uncheck the checkbox in javascript.

Sheng Jiang 蒋晟
This all seems very overcomplicated...
Telos
It would be more complicated if you skip .Net's wrapper classes and call IE API directly.
Sheng Jiang 蒋晟
My point is that once you've got the document in the webbrowser control you can manipulate the dom objects directly, without resorting to javascript.
Telos
For instance, see the edit to my answer...
Telos
A: 

The LONG version is that you'll have to use DOM functions to find the client ID of the control you want to update, then just set it's value.

I can probably find some sample code for you once I get to work if no one else beats me to it...

EDIT:

Basically all you need to do is:

HTMLDocumentClass doc = webBrowserControl.Document as mshtml.HTMLDocumentClass;

HTMLInputElement input = doc.all.item("checkboxID", 0) as HTMLInputElement;

input.value ="false";  //Could be 0, not sure... 
//could also be input.@checked = false, sorry I haven't actually used this on a checkbox before... one of these should work though

So yeah, not sure why anyone would go through all the effort to write a JavaScript and invoke it when you can set a control's value with 3 lines of code.

Telos
It should be `input.@checked = false;`. And using JS you don't have to reference mshtml and you can do this in one line - `this.webBrowser1.Document.InvokeScript( "uncheck" );`.
Majkel
Well, one line in C# + one in JS.
Majkel
Well I _could_ condense it down to one line of C# if I really wanted to. ;) I suspect actually adding the script to the page and using it would end up being more.
Telos
Solution with mshtml in my answer actually is one line ;) And JS approach to be one line would require access to the page source - otherwise it would be necessary to use mshtml API to insert your script.
Majkel
A: 

You can use the InvokeMember Method of a HTML Element to invoke a DOM Event. In our case "click". If you have a WebBrowser Control than all you have to do is find the element and Invoke the Method.

For Each ele As HtmlElement In Me.WebBrowser1.Document.All
        If ele.Name = "accept" Then
            ele.InvokeMember("click")
        End If
Next

In my example 'accept' was the name of the checkbox element. You can use the SetAttribute() for some other controls but the best way to emulate a click is to find what the checkbox state is by using GetAttribute() and click if the checkbox is set to false. Hope it helps.

Justin