簡體   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