【问题标题】:How to insert an HTML table into a WebBrowser control C#如何将 HTML 表格插入 WebBrowser 控件 C#
【发布时间】:2011-02-10 14:17:38
【问题描述】:

我使用 .NET WebBrowser 控件作为所见即所得的 html 编辑器。到目前为止,我一直在使用 ExecCommand 来执行格式化功能,但是我现在想添加一个表格插入器。问题是我似乎只能将表格附加到文档中,而不是中途插入。下面是一些基本的测试代码,如果有人能提供帮助,我将不胜感激。

        HtmlElement tableRow = null;
        HtmlElement headerElem = null;

        HtmlDocument doc = wbDesign.Document;
        HtmlElement tableElem = doc.CreateElement("TABLE");
        doc.Body.AppendChild(tableElem);

        HtmlElement tableHeader = doc.CreateElement("THEAD");
        tableElem.AppendChild(tableHeader);
        tableRow = doc.CreateElement("TR");
        tableHeader.AppendChild(tableRow);
        headerElem = doc.CreateElement("TH");
        headerElem.InnerText = "Col1";
        tableRow.AppendChild(headerElem);

        headerElem = doc.CreateElement("TH");
        headerElem.InnerText = "Col2";
        tableRow.AppendChild(headerElem);

        HtmlElement tableBody = doc.CreateElement("TBODY");
        tableElem.AppendChild(tableBody);

        tableRow = doc.CreateElement("TR");
        tableBody.AppendChild(tableRow);
        HtmlElement tableCell = doc.CreateElement("TD");
        tableCell.InnerText = "Test";
        tableRow.AppendChild(tableCell);
        tableCell = doc.CreateElement("TD");
        tableCell.InnerText = "Test";
        tableRow.AppendChild(tableCell);

【问题讨论】:

    标签: c# html .net browser html-table


    【解决方案1】:

    您需要导航 HtmlDocument 结构,找到要插入它的节点,然后在此处追加。如果您追加到正文,您只需添加到最后一个元素的末尾,即末尾。

    【讨论】:

    • 您知道如何根据当前选择来执行此操作吗?
    • 您需要帮助查找 IHTMLDocument2::selection 控制范围内的最后一个元素吗?它有一个长度属性。
    • 我的问题是我似乎无法将表格插入到当前选择中?!?!
    【解决方案2】:

    有点晚了-但我最近有这个要求并想出了这个。我已尝试将其尽可能简化,以展示方法,并允许您根据需要进行自定义。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.UI.HtmlControls;
    using System.Windows.Forms;
    using System.IO;
    using System.Web.UI;
    
    public class HtmlToWebBrowserControlDoc
    {
    
        // The loaded document MUST have a script similar to this
    
        //      <script type="text/javascript" >
        //              function appendHtml(o) {
        //              var div = document.createElement("div");
        //              div.innerHTML = o;
        //              document.body.appendChild( div);
        //              }
        //      </script>
    
    
        public static void InsertHtmlControl(HtmlControl c, WebBrowser wb)
        {
    
            // create a HtmlTextWriter;
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            HtmlTextWriter htmlw = new HtmlTextWriter(sw);
    
            // render the control as html
            c.RenderControl(htmlw);
    
    
            //invoke the script passing in the html 
            object[] p = new object[1];
            p[0] = (object)sb.ToString();
            wb.Document.InvokeScript("appendHtml", p);
    
            htmlw.Close();
            htmlw.Dispose();
    
            sw.Dispose();
    
    
        }
    }
    

    【讨论】:

      【解决方案3】:
      [DllImport("user32.dll")]
      public static extern bool GetCaretPos(ref Point pt);
      
      .....
      
       HtmlElement newElement = webBrowser.Document.CreateElement("<div></div>");
       Point p = new Point();
       GetCaretPos(ref p);
       HtmlElement currentElement = webBrowser.Document.GetElementFromPoint(p);
       currentElement.InsertAdjacentElement(HtmlElementInsertionOrientation.AfterBegin, newElement);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-30
        • 2013-12-01
        • 1970-01-01
        • 2020-02-04
        相关资源
        最近更新 更多