3

In my winforms aplication I have a WebBrowser control named webBrowser1.

In code all I added is navigating to a page:

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

The code for the page to which I navigate is:

<%@ 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>

Seen in a browser like IE or Chrome the page looks like this:

Clicking on Add Destination anchor creates a new input:

The problem that I'm having with WebBrowser control is that it loads the page but the JavaScript doesn't work.

If I click on Add Destination nothing happens, even though the same page works well in Chrome or IE.

Placing a breakpoint and using:

webBrowser1.Document.InvokeScript("addDestination");

inside the Immediate window and then continuing to run the program activates the JavaScript in that function adding a new input.

Thanks for replies!

asked Sep 8, 2011 at 8:29

2 Answers 2

2

Try attaching the click handlers like this instead of using setAttribute in the onload and attach functions:

document.getElementById('addDestination').onclick = addDest;
answered Sep 8, 2011 at 8:36
Sign up to request clarification or add additional context in comments.

1 Comment

That fixed the problem. Any ideea why it doesn't work using setAttribute since in other browsers it works? I am using IE8 in windows and it works. Doesn't WebBrowser use IE8 also (latest IE installed)?
0

You could try setting the ScriptErrorsSuppressed property to false to see whether any JavaScript errors occur.

answered Sep 8, 2011 at 8:33

2 Comments

I tried setting it to false (added that before the navigate line) but no errors appeared. Where are the errors supposed to be seen?
From what I know, a message box should appear.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.