HtmlDocument.CreateElement(String) 方法

定义

新建一个指定 HTML 标记类型的 HtmlElement

public:
 System::Windows::Forms::HtmlElement ^ CreateElement(System::String ^ elementTag);
public System.Windows.Forms.HtmlElement CreateElement (string elementTag);
public System.Windows.Forms.HtmlElement? CreateElement (string elementTag);
member this.CreateElement : string -> System.Windows.Forms.HtmlElement
Public Function CreateElement (elementTag As String) As HtmlElement

参数

elementTag
String

要创建的 HTML 元素的名称。

返回

指定标记类型的新元素。

示例

下面的代码示例使用 Northwind 数据库中的数据通过 创建 HTML 表 CreateElementAppendChild还使用 方法,首先将单元格 (TD元素) 添加到) 的行 (TR元素,然后将行添加到表中,最后将表追加到当前文档的末尾。 该代码示例要求应用程序具有名为 WebBrowserWebBrowser1控件。

private void DisplayCustomersTable()
{
    DataSet customersSet = new DataSet();
    DataTable customersTable = null;
    SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Customers", "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;");
    sda.Fill(customersTable);
    customersTable = customersSet.Tables[0];

    if (webBrowser1.Document != null)
    {
        HtmlElement tableRow = null;
        HtmlElement headerElem = null;

        HtmlDocument doc = webBrowser1.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);

        foreach (DataColumn col in customersTable.Columns)
        {
            headerElem = doc.CreateElement("TH");
            headerElem.InnerText = col.ColumnName;
            tableRow.AppendChild(headerElem);
        }

        // Create table rows.
        HtmlElement tableBody = doc.CreateElement("TBODY");
        tableElem.AppendChild(tableBody);
        foreach (DataRow dr in customersTable.Rows)
        {
            tableRow = doc.CreateElement("TR");
            tableBody.AppendChild(tableRow);
            foreach (DataColumn col in customersTable.Columns)
            {
                Object dbCell = dr[col];
                HtmlElement tableCell = doc.CreateElement("TD");
                if (!(dbCell is DBNull))
                {
                    tableCell.InnerText = dbCell.ToString();
                }
                tableRow.AppendChild(tableCell);
            }
        }
    }
}
Private Sub DisplayCustomersTable()
    ' Initialize the database connection.
    Dim CustomerData As New DataSet()
    Dim CustomerTable As DataTable

    Try
        Dim DBConn As New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;")
        Dim DBQuery As New SqlDataAdapter("SELECT * FROM CUSTOMERS", DBConn)
        DBQuery.Fill(CustomerData)
    Catch dbEX As DataException

    End Try

    CustomerTable = CustomerData.Tables("Customers")

    If (Not (WebBrowser1.Document Is Nothing)) Then
        With WebBrowser1.Document
            Dim TableElem As HtmlElement = .CreateElement("TABLE")
            .Body.AppendChild(TableElem)

            Dim TableRow As HtmlElement

            ' Create the table header. 
            Dim TableHeader As HtmlElement = .CreateElement("THEAD")
            TableElem.AppendChild(TableHeader)
            TableRow = .CreateElement("TR")
            TableHeader.AppendChild(TableRow)

            Dim HeaderElem As HtmlElement
            For Each Col As DataColumn In CustomerTable.Columns
                HeaderElem = .CreateElement("TH")
                HeaderElem.InnerText = Col.ColumnName
                TableRow.AppendChild(HeaderElem)
            Next

            ' Create table rows.
            Dim TableBody As HtmlElement = .CreateElement("TBODY")
            TableElem.AppendChild(TableBody)
            For Each Row As DataRow In CustomerTable.Rows
                TableRow = .CreateElement("TR")
                TableBody.AppendChild(TableRow)
                For Each Col As DataColumn In CustomerTable.Columns
                    Dim Item As Object = Row(Col)
                    Dim TableCell As HtmlElement = .CreateElement("TD")
                    If Not (TypeOf (Item) Is DBNull) Then
                        TableCell.InnerText = CStr(Item)
                    End If
                    TableRow.AppendChild(TableCell)
                Next
            Next

        End With
    End If
End Sub

注解

elementTag可以是 Internet Explorer 中支持的任何 HTML 标记,但 和 IFRAME除外FRAME

CreateElement 返回未附加到当前文档树的元素。 若要将 元素添加到文档,请使用 InsertAdjacentElementAppendChild 方法。

使用 WebBrowser 控件的 “查看源 ”上下文菜单命令或 DocumentText 控件的 和 DocumentStream 属性时,此方法不会影响现有文档源代码 WebBrowser 的状态。

使用 CreateElement创建新元素时,将无法设置某些属性,例如 Name。 如果需要设置 Name 属性,请将其作为 HTML 分配给 InnerHtml 文档中另一个 对象的 属性。

适用于

另请参阅