【问题标题】:Setting innerHtml property of web browser control in windows form在windows窗体中设置web浏览器控件的innerHtml属性
【发布时间】:2014-01-08 13:14:30
【问题描述】:

我正在使用 Web 浏览器控件加载包含在标签中的一些 HTML 内容的 windows 窗体。

现在当我尝试设置

的值时
       WebBrowserCntrl.Navigate("about:blank");
       HtmlDocument doc = this.WebBrowserCntrl.Document;
       doc.Write(String.Empty);
WebBrowserCntrl.Document.Body.InnerHTML= datacontractclass.datamember(string)

它作为“对象引用未设置为对象的实例”引发异常。 但是当我这样做时..

WebBrowserCntrl.DocumentText="<style>....</style>" WebBrowserCntrl.DocumentText= datacontractclass.datamember(string) 有用。 但是后来我无法更改 webbrowser 的样式。(就像我上面应用的任何样式)

为什么我会收到此异常,或者是否有任何其他方式可以附加样式和其他 HTMl 内容。

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    你可以试试这个:

    webBrowser.DocumentText = string.Empty; // reset whatever is in the browser
    
    // Either this:
    HtmlDocument doc = webBrowser.Document.OpenNew(true);
    doc.Write(yourHtml);
    webBrowser.Refresh();
    
    // Or:
    webBrowser.Document.OpenNew(true);
    webBrowser.Document.Write(html);
    webBrowser.Refresh();
    

    如果您需要在其中设置样式,您可以使用HtmlTextWriter 创建您的 html。例如,如果您想使用外部 css 文件,可以在 head 部分执行此操作:

    htw.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
    htw.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
    htw.AddAttribute(HtmlTextWriterAttribute.Href, pathToCssFile);
    htw.RenderBeginTag(HtmlTextWriterTag.Link);
    htw.RenderEndTag();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2013-10-27
      相关资源
      最近更新 更多