在C#中,如何用webbrowser控件获取网页内的一个变量的值?如何获取一个控件的值呢?比如文本框的值。

解决方案 »

  1.   

    可以的,请看:
    如果要通过 HTML 文档对象模型 (DOM) 访问显示在 WebBrowser 控件中的网页的内容,则使用WebBrowser.Document属性。例如,如果要在 Windows 窗体应用程序中使用基于 Web 的控件,此属性十分有用。可以将WebBrowser.Document属性与 WebBrowser.ObjectForScripting 属性组合使用,以便在 WebBrowser 控件中显示的网页与应用程序之间实现双向通信。使用 HtmlDocument.InvokeScript 方法从客户端应用程序代码调用网页中实现的脚本方法。脚本代码可以通过 window.external 对象访问应用程序,该对象是用于主机访问的内置 DOM 对象,它映射到为 ObjectForScripting 属性指定的对象。下面的代码示例演示如何在 Navigating 事件的处理程序中使用 Document 属性确定是否已填写了网页窗体。如果输入字段不包含任何值,则取消导航。
    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.DocumentText =
            "<html><body>Please enter your name:<br/>" +
            "<input type='text' name='userName'/><br/>" +
            "<a href='http://www.microsoft.com'>continue</a>" +
            "</body></html>";
        webBrowser1.Navigating += 
            new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
    }private void webBrowser1_Navigating(object sender, 
        WebBrowserNavigatingEventArgs e)
    {
        System.Windows.Forms.HtmlDocument document =
            this.webBrowser1.Document;    if (document != null && document.All["userName"] != null && 
            String.IsNullOrEmpty(
            document.All["userName"].GetAttribute("value")))
        {
            e.Cancel = true;
            System.Windows.Forms.MessageBox.Show(
                "You must enter your name before you can navigate to " +
                e.Url.ToString());
        }
    }
      

  2.   

    那如果是想要在本地windows程序中读取远程的一个文本呢?
    例如我想要在程序中读取www.fujiangjiang.com/test.txt的内容。应该如何实现?我新手,没有代码给各思想也行。