problem using winforms WebBrowser in asp.net

筅森魡賤 提交于 2019-12-23 04:29:53

问题


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?


回答1:


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..




回答2:


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.



来源:https://stackoverflow.com/questions/4461420/problem-using-winforms-webbrowser-in-asp-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!