donetbrowser HOWTO从Dotnetbrowser获取发布的表单数据?

时间:2017-10-14 16:14:10

标签: c# html post postdata dotnetbrowser

我需要帮助才能获得Dotnetbrowser发布的表单数据。 事实上,我想在按钮上添加一个监听器事件,当点击这个按钮时,我得到了数据,例如:

<!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>

当我点击提交按钮时,我从“txtSomething”获取值。 或者当dotnetbrowser检测到表单提交时,我从“txtSomething”中获取值。

提前致谢。 ;)

1 个答案:

答案 0 :(得分:2)

您可以尝试为相应的DOM元素注册OnClick事件侦听器,以便获得有关点击的通知。

在您的情况下,可以按如下所示完成:

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>");

以下文章详细说明了如何注册DOM事件监听器: https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000110034-listening-to-dom-events

此外,还有一篇文章介绍了如何以编程方式设置表单字段的值:https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000110038-setting-input-field-value-working-with-form 。示例中的ValueChecked属性是与JavaScript中可用的valuechecked属性相对应的读写属性。