【问题标题】:VB.NET SetAttribute in WebBrowser doens't workWebBrowser 中的 VB.NET SetAttribute 不起作用
【发布时间】:2018-06-07 16:14:16
【问题描述】:

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

附言: 我的应用程序是实时构建的,屏幕上没有浏览器控件,它是在读取 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)

【问题讨论】:

  • 为什么要导航到文件然后创建新文档只是为了再次加载文件?删除后者并坚持只导航到它。

标签: vb.net forms visual-studio-2010 webbrowser-control setattribute


【解决方案1】:

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

'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)

【讨论】:

  • 非常感谢,我在这卡了2天!一个愚蠢的逻辑错误哈哈哈
  • @CarlitoMurta :很高兴我能帮上忙!祝你的项目好运! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 2013-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多