I need help to get posted form data a Dotnetbrowser. In fact, I want to add a listener event on a button, when this button is clicked, so I get the data, for example:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<body>
<h1>Hello!!!</h1><br>
<form
id="formTrying"
method="post"
action="http://localhost/MyWebSite/MyMvcView?MyQueryStringParam1=something&Param2=something"
>
<input name="txtSomething" type="text" value="Trying something..."><br>
<input type="submit" value="Submit...">
</form>
</body>
</html>
When I click on submit button, I get the value from "txtSomething". Or when the dotnetbrowser detect form submit, I get the value from "txtSomething".
Thanks in advance. ;)
You can try registering an OnClick
event listener for the corresponding DOM element to be notified about the clicks.
In your case, this can be done as shown below:
browser.FinishLoadingFrameEvent += delegate (object sender, FinishLoadingEventArgs e)
{
if (e.IsMainFrame)
{
DOMDocument document = e.Browser.GetDocument();
List<DOMNode> inputNodes = document.GetElementsByTagName("input");
DOMInputElement submitElement = (DOMInputElement)inputNodes.LastOrDefault();
DOMEventHandler domEvent = delegate (object s, DOMEventArgs args)
{
//Obtain the value of the txtSomething field
DOMInputElement txtSomethingElement = (DOMInputElement)document.GetElementByName("txtSomething");
string value = txtSomethingElement.Value;
Console.Out.WriteLine("txtSomething value = " + value);
};
submitElement.AddEventListener(DOMEventType.OnClick, domEvent, false);
}
};
browser.LoadHTML(@"<!DOCTYPE html>
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<body>
<h1>Hello!!!</h1><br>
<form
id='formTrying'
method='post'
action='http://localhost/MyWebSite/MyMvcView?MyQueryStringParam1=something&Param2=something'
>
<input name='txtSomething' type='text' value='Trying something...'><br>
<input type='submit' value='Submit...'>
</form>
</body>
</html>");
The following article explains in details how to register a DOM event listener: https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000110034-listening-to-dom-events
Also, there is an article that shows how to set the values of the form fields programmatically: https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000110038-setting-input-field-value-working-with-form
. The Value
and Checked
properties in the sample are read-write properties that correspond to the value
and checked
properties available in JavaScript.