简体   繁体   English

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

[英]How to insert an HTML table into a WebBrowser control C#

I am using the .NET WebBrowser control to as a WYSIWYG html editor. 我使用.NET WebBrowser控件作为WYSIWYG html编辑器。 I've been using the ExecCommand to do the formatting function fine so far, however I now want to add a table inserter. 到目前为止,我一直在使用ExecCommand来完成格式化功能,但是现在我想添加一个表插入器。 The problem is I can only seem to Append the table to the document, not insert it half way through. 问题是我似乎只能将表追加到文档,而不能将其插入到一半。 Below is some basic test code, if anyone can help, I'd appreciate it. 以下是一些基本的测试代码,如果有人可以提供帮助,我将不胜感激。

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

You need to navigate the HtmlDocument structure, finding the node at where you want to insert it, and then append there. 您需要导航HtmlDocument结构,找到要在其中插入节点的节点,然后将其追加到该位置。 If you Append to the Body, you'll just add to the end of the last element, ie the end. 如果您附加到主体,则只需将其添加到最后一个元素的末尾,即末尾。

Its a bit late - but I recently had this requirment and came up with this. 有点晚了-但是我最近有这个要求,并提出了这个要求。 I've tried to make this a minimal as possible, to show the methodology, and allow you to customise this a required. 我试图将其最小化,以显示方法,并允许您自定义此要求。

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


    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM