繁体   English   中英

在托管WebBrowser控件的应用程序中,如何在由WebBrowser控件查看的页面中调用JavaScript函数?

[英]How might I call a JavaScript Function in a Page Viewed by a WebBrowser Control from the Application Hosting the WebBrowser Control?

我创建了一个C#桌面应用程序,该应用程序依赖于Web搜寻器的想法。

我使用网络浏览器控件制作了我的应用程序,以打开网站并进行编程登录,然后重定向到具有gridview的特定页面,其中包含我要收集的所有用户数据...

但是,这里的问题是JavaScript函数触发了gridview中的用户名。 我知道它的名称,但不知道如何在桌面应用程序中调用它。

什么是允许我这样做的名称空间或DLL?

我认为这应该可以帮助您:

http://www.west-wind.com/WebLog/posts/493536.aspx

编辑:基于该链接,这是一个小示例应用程序。 将按钮和WebBrowser控件添加到Windows窗体,并添加以下代码:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.webBrowser1.DocumentText = @"<html>
<body>
<script type = ""text/javascript"">
function ShowMessage(text) {
   alert(text);
}
</script>
</body>
<input type=""button"" onclick=""ShowMessage('From JavaScript')"" value=""Click me""/>
</html>";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Document.InvokeScript("ShowMessage",new object[]{"From C#"});
        }
    }

如果单击浏览器中的按钮,它将显示javascript消息,如果单击Windows窗体中的按钮,则将显示C#消息。

您是否考虑过仅使用以下内容通过WebBrowser控件在页面上运行脚本?

' In VB - but easy to convert to C# as its pretty much the same thing :).
Dim sScript As String, sLanguage As String
sLanguage = "JScript"
sScript = "MyJavaScriptToRun();" ' you can run many lines by delimiting them with ;

WebBrowser1.Document.parentWindow.execScript sScript, sLanguage

让我知道你的相处。 在.NET版本的WebBrowser控件中,还有其他运行脚本的方法,我认为有一种.RunScript类型的方法(无法记住确切的名称,但是一旦在intellisense中看到它,您就会注意到它)。

如果您还有其他问题,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM