2011-09-08 56 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

据我所知,应该会出现一个消息框。 –

相关问题