如何将HTML表插入WebBrowser控件C#

时间:2010-04-21 12:47:38

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

我使用.NET WebBrowser控件作为WYSIWYG 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);

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

[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);

答案 2 :(得分:0)

它有点晚了 - 但我最近有了这个要求并想出了这个。我试图尽量减少这个,以显示方法,并允许您自定义这个。

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();


    }
}