0

i am using the WebBrowser control in asp.net page. here is the simple code:

Public Class _Default
    Inherits System.Web.UI.Page

    Private WithEvents browser As WebBrowser
    Dim th As New Threading.Thread(AddressOf ThreadStart)

    Sub ThreadStart()
        browser = New WebBrowser
        AddHandler browser.DocumentCompleted, AddressOf browser_DocumentCompleted
        browser.Navigate("http://www.someurl.com/")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        th.SetApartmentState(Threading.ApartmentState.STA)
        th.Start()
        th.Join()
    End Sub

    Private Sub browser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
        If browser.Document IsNot Nothing Then
                Dim textbox As HtmlElement = browser.Document.GetElementById("txt1")
                textbox.InnerText = "some text"
                Dim button As HtmlElement = browser.Document.GetElementById("btn1")
                button.InvokeMember("click")
        End If
    End Sub
End Class

the problem is that the webbrowser's DocumentCompleted event is not being handled. It looks like the page request finishes before anything else could happen. what's the solution to this problem?

Ali Tarhini
  • 5,278
  • 6
  • 41
  • 66

2 Answers2

2

I really recommend reading this article(He won a price for it..)

Using the WebBrowser Control in ASP.NET http://www.codeproject.com/KB/aspnet/WebBrowser.aspx

His solution is to create 3 threads for it to work..

StefanE
  • 7,578
  • 10
  • 48
  • 75
-1

I'm not sure but I have some concerns about the way you wrote your code.

You are creating and initializing your thread as soon as your class instance is created. This is before the form has been loaded.

I can't say for sure this couldn't work but I would definitely recommend creating the thread in your Load event handler, just before you use it.

I wrote some similar code in C# to generate a website thumbnail. Although that code does not use the DocumentCompleted event, I played with that event when I wrote it and it seemed to work okay. You can compare my code to yours.

Also, I should mention I have one hosting account where the code doesn't work. It seems to simply die when I call Thread.Join. However, it doesn't appear that's the issue you're running into.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • I recommend reading my comment above. I specifically stated that I don't use it but that it worked when I tried it. Of course, you are also free to completely ignore what I wrote... – Jonathan Wood Dec 16 '10 at 14:34
  • reread what you wrote: Although that code does use the DocumentCompleted event – Ali Tarhini Dec 16 '10 at 14:43
  • Huh? Which code uses DocumentCompleted? And why do you keep going on about that when I addressed it in my original answer? Did you try my suggestion or not? – Jonathan Wood Dec 16 '10 at 14:50
  • because you mentioned that you used it and in your post i cant find it! – Ali Tarhini Dec 16 '10 at 15:03
  • I see. Yes, that was a mistype, which I've now corrected. No reason to ignore my suggestion though. (Twice now.) – Jonathan Wood Dec 16 '10 at 16:45