2010-04-21 50 views
1

我正在使用.NET WebBrowser控件作爲所見即所得的html編輯器。我一直在使用ExecCommand來完成格式化功能,但是現在我想添加一個表格插入器。問題是我似乎只能將表追加到文檔中,而不是插入到文檔中。以下是一些基本的測試代碼,如果有人可以幫忙,我會很感激。如何將HTML表格插入WebBrowser控件C#

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

回答

1

您需要瀏覽HtmlDocument結構,找到要插入它的位置的節點,然後附加到那裏。如果您追加到正文,您只需添加到最後一個元素的末尾,即結尾。

+0

任何想法,你是怎麼做到這基於當前的選擇? – 2010-04-21 15:14:39

+0

你需要幫助找到IHTMLDocument2 :: selection控制範圍中的最後一個元素?它有一個長度屬性。 – 2010-04-21 17:45:24

+1

我遇到的問題是我似乎無法將表格插入當前選區?!?! – 2010-05-28 12:12:09

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


    } 
} 
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); 
相關問題