1

Hi is there a way to submit a form WITHOUT clicking the submit button?

enter image description here

Keep in mind this is inside the webBrowser1 control in C# . So if i click a button in my C# application it should automatically submit the form.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
IceDawg
  • 327
  • 4
  • 13
  • 21

2 Answers2

0

You can inject Javascript code that submits the form to the page:

// Do this on form load.
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function submitForm() { document.getElementById('formId').submit(); }";
head.AppendChild(scriptEl);

Then invoke it on button click:

// In button click handler
webBrowser1.Document.InvokeScript("submitForm");
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
  • @ Adi Lester : what is IHTMLScriptElement ? – IceDawg May 18 '13 at 14:34
  • It's an interface representing an HTML script. If you're having problems finding it, try [this link](http://stackoverflow.com/a/5216255/389966) – Adi Lester May 18 '13 at 14:38
  • private void Form1_Load(object sender, EventArgs e) { HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0]; HtmlElement scriptEl = webBrowser1.Document.CreateElement("script"); IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement; element.text = "function submitForm() { document.getElementByName('sendmessage').submit(); }"; head.AppendChild(scriptEl); } webBrowser1.Document.InvokeScript("submitForm"); – IceDawg May 18 '13 at 14:40
  • Keep in mind you do need to change "formId" in the script to the actual id of the form. You can find out what it is by looking at the page's source. – Adi Lester May 18 '13 at 14:40
  • Try replacing `document.getElementByName('sendmessage').submit();` with `document.getElementsByName('sendmessage')[0].value.submit();` – Adi Lester May 18 '13 at 14:43
0

Another Way:

Webbrowser1.navigate("javascript:document.getElementById('FormId').submit();")

There are many ways to do it, if this code not working I'll bring you more examples.

LC123
  • 13
  • 1
  • 3