Redigera

Dela via


HtmlDocument.CreateElement(String) Method

Definition

Creates a new HtmlElement of the specified HTML tag type.

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

Parameters

elementTag
String

The name of the HTML element to create.

Returns

A new element of the specified tag type.

Examples

The following code example uses data from the Northwind database to create an HTML table using CreateElement. The AppendChild method is also used, first to add cells (TD elements) to rows (TR elements), then to add rows to the table, and finally to append the table to the end of the current document. The code example requires that your application has a WebBrowser control called WebBrowser1.

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

Remarks

elementTag may be any of the supported HTML tags in Internet Explorer except for FRAME and IFRAME.

CreateElement returns an element unattached to the current document tree. To add the element to the document, use either the InsertAdjacentElement or AppendChild methods.

This method will not affect the state of an existing document's source code when you use the WebBrowser control's View Source context menu command or the DocumentText and DocumentStream properties of the WebBrowser control.

When you create new elements with CreateElement, you will not be able to set certain properties, such as Name. In cases where you need to set the Name attribute, assign them as HTML to the InnerHtml property of another object in the document.

Applies to

See also