2011-09-08 60 views
3

在我的WinForms aplication我有一個名爲webBrowser1 WebBrowser控件。我怎樣才能讓JavaScript的web瀏覽器中運行的控制?

在代碼中的所有我添加被導航到的網頁:

private void Form1_Load(object sender, EventArgs e) 
{ 
    webBrowser1.Navigate("http://localhost:6489/Default.aspx"); 
} 

用於向我瀏覽頁面中的代碼是:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TableRowShow.Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script type="text/javascript"> 
     window.onload = function() 
     { 
     document.getElementById('addDestination').setAttribute('onclick', 'addDest();'); 
     } 

     function attach() 
     { 
     document.getElementById('addDestination').setAttribute('onclick', 'addDest();'); 
     } 

     var i = 1; // position of next tr to be shown 

     function addDest() 
     { 
     var trs = document.getElementById('travelTable').getElementsByTagName('tr'); 

     if (trs[i] != null) 
      trs[i++].style.display = ""; 
     } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <table id="travelTable"> 
     <tr> 
      <td> 
      <asp:TextBox runat="server" /> 
      </td> 
     </tr> 
     <tr style="display: none"> 
      <td> 
      <asp:TextBox runat="server" /> 
      </td> 
     </tr> 
     <tr style="display: none"> 
      <td> 
      <asp:TextBox runat="server" /> 
      </td> 
     </tr> 
     </table> 
     <asp:HyperLink runat="server" ID="addDestination" 
     ClientIDMode="Static" NavigateUrl="javascript:;" > 
     Add Destination 
     </asp:HyperLink> 
    </form> 
</body> 
</html> 

見於如IE或鉻的頁面的瀏覽器看起來是這樣的:

點擊添加目標錨創建一個新的輸入:

,我要和WebBrowser控件遇到的問題是,它加載頁面,但JavaScript不工作。

如果我點擊添加目標沒有任何反應,即使在同一個頁面效果很好Chrome或IE瀏覽器。

放置一個斷點,並使用:

webBrowser1.Document.InvokeScript("addDestination"); 

立即窗口內,然後繼續運行該程序啓動的JavaScript在該函數中增加一個新的輸入。

感謝您的回覆!

回答

2

嘗試連接該單擊處理這樣的,而不是在onloadattach功能使用setAttribute

document.getElementById('addDestination').onclick = addDest; 
+0

解決了這個問題。任何ideea爲什麼它不起作用使用setAttribute,因爲在其他瀏覽器中它工作?我在Windows中使用IE8,它的工作原理。不WebBrowser也使用IE8(最新的IE安裝)? –

0

你可以嘗試ScriptErrorsSuppressed屬性設置爲false,看是否出現任何JavaScript錯誤。

+0

我想它設置爲false(添加的導航線之前),但是沒有錯誤出現。應該在哪裏看到錯誤? –

+0

據我所知,應該會出現一個消息框。 –

相關問題