VB.NET WebBrowser 中的 SetAttribute 不起作用

VB.NET SetAttribute in WebBrowser doens't work

我之前做了一些研究,但所有的答案都不有效。 "value" 属性存在于元素中,但不会 出现 在 webBrowser 中,也不在输入中。 这是我的代码,我需要 webBrowser 读取 html 文件,然后从数据库的输入中加载答案或值。

PS: 我的应用程序是实时构建的,屏幕上没有网络浏览器控件,它是在读取 html 文件后不久创建的,然后才放在面板中。

    Dim webBrowser As WebBrowser = New WebBrowser
    Dim _doc As HtmlDocument
    Dim htmlPath As String = "C:\ePrimeCare\Platis\Debug\Protocolos\" + 
    nomeProtocolo + "_" + idSistema.ToString() + ".html"
    webBrowser.ScriptErrorsSuppressed = True
    webBrowser.Navigate(htmlPath)
    _doc = webBrowser.Document.OpenNew(False)
    'webBrowser.DocumentText = IO.File.ReadAllText(htmlPath).ToString()
    'webBrowser.Document.OpenNew(False)
    'RetornaRespostasAnteriores(idSistema, idFicha, nomeProtocolo, _doc, Convert.ToDateTime(dtVisita))
    _doc.Title = nomeProtocolo
    _doc.Write(IO.File.ReadAllText(htmlPath).ToString())
    Dim carregaRespostas As CarregarRespostaProtocoloHTML = New CarregarRespostaProtocoloHTML
    Dim respostas As DataTable = carregaRespostas.BuscarRespostasProtocoloAnterior(idFicha, idSistema, dtVisita)

    Dim idopcaoitem As String = 0
    Dim idsetitem As String = 0
    Dim value As DataRow()
    Dim strArr As String()
    For Each element As HtmlElement In _doc.GetElementsByTagName("input")
        Dim type As String = element.GetAttribute("type")
        Select Case type
            Case "text"
                strArr = element.GetAttribute("id").Split("_") 'For get the two ids
                idopcaoitem = strArr(0)
                value = respostas.Select(("IDOPCAOITEM = " + idopcaoitem.ToString()))
                If value.Length > 0 Then
                    element.SetAttribute("value", value(0)(2).ToString())'Here i try to set the value, but does not work
                End If
            Case "radio"
                Debug.WriteLine("Input de radio")
            Case "checkbox"
                Debug.WriteLine("Input de checkbox")
            Case "hidden"
                Debug.WriteLine("Input de hidden")
            Case Else
                Debug.WriteLine("Outro input")
        End Select
    Next
    _doc.Write(IO.File.ReadAllText(htmlPath).ToString())
    webBrowser.Refresh(WebBrowserRefreshOption.Completely)
    webBrowser.Dock = Dock.Fill
    pnlMain.Controls.Add(webBrowser)

您所有的文档重写和最后的刷新将覆盖您对其所做的任何更改。

'Either of these will revert the document back to its original state.
_doc.Write(IO.File.ReadAllText(htmlPath).ToString())
webBrowser.Refresh(WebBrowserRefreshOption.Completely)

您甚至不需要调用 _doc.Write(),因为 WebBrowser1.Navigate(htmlPath) 也能正常工作。

新代码:

Dim webBrowser As New WebBrowser 'Shorthand statement.
Dim _doc As HtmlDocument
Dim htmlPath As String = "C:\ePrimeCare\Platis\Debug\Protocolos\" + 
nomeProtocolo + "_" + idSistema.ToString() + ".html"
webBrowser.ScriptErrorsSuppressed = True
webBrowser.Navigate(htmlPath)
_doc = webBrowser.Document 'Removed OpenNew().
_doc.Title = nomeProtocolo

Dim carregaRespostas As New CarregarRespostaProtocoloHTML 'Another shorthand statement.
Dim respostas As DataTable = carregaRespostas.BuscarRespostasProtocoloAnterior(idFicha, idSistema, dtVisita)

(...your variables...)

For Each element As HtmlElement In _doc.GetElementsByTagName("input")

    (...your code...)

Next

'(Removed _doc.Write() and Refresh() since they will undo all changes)

webBrowser.Dock = Dock.Fill
pnlMain.Controls.Add(webBrowser)