简体   繁体   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?

I've created a C# Desktop Application which depends on web crawler idea. 我创建了一个C#桌面应用程序,该应用程序依赖于Web搜寻器的想法。

I made my application using web browser control to open a website and login progmatically and redirect to the specific page which have a gridview with all user's data that I want to collect... 我使用网络浏览器控件制作了我的应用程序,以打开网站并进行编程登录,然后重定向到具有gridview的特定页面,其中包含我要收集的所有用户数据...

But the issue here username in the gridview click fired by JavaScript function. 但是,这里的问题是JavaScript函数触发了gridview中的用户名。 I know its name but not how to call it inside desktop application. 我知道它的名称,但不知道如何在桌面应用程序中调用它。

What are the namespaces or DLLs that would allow me to do so? 什么是允许我这样做的名称空间或DLL?

I think this should help you out: 我认为这应该可以帮助您:

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

EDIT: Based on that link, here's a small sample app. 编辑:基于该链接,这是一个小示例应用程序。 Add a Button and a WebBrowser control to a Windows Form and add this code: 将按钮和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#"});
        }
    }

If you click the button in the browser, it'll show the javascript message, if you click the button in the windows form, it'll show the C# message. 如果单击浏览器中的按钮,它将显示javascript消息,如果单击Windows窗体中的按钮,则将显示C#消息。

Have you thought of just using the following to run script on page using WebBrowser control? 您是否考虑过仅使用以下内容通过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

Let me know how you get along. 让我知道你的相处。 In the .NET versions of the WebBrowser control, there are additional methods to run script, I think there is a .RunScript type method (can't recall exact name but you will notice it once you see it in intellisense). 在.NET版本的WebBrowser控件中,还有其他运行脚本的方法,我认为有一种.RunScript类型的方法(无法记住确切的名称,但是一旦在intellisense中看到它,您就会注意到它)。

Let me know if you have any further questions. 如果您还有其他问题,请告诉我。

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

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