HtmlDocument.CreateElement(String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立指定的 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 表格 CreateElement。 此 AppendChild 方法也被使用,先將儲存格(TD 元素)加入列(TR 元素),再將資料列加入表格,最後將表格附加到目前文件末尾。 程式碼範例要求你的應用程式有一個 WebBrowser 名為 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
備註
elementTag除了 和 IFRAME外,都可以是 Internet Explorer FRAME 中支援的任何 HTML 標籤。
CreateElement 回傳一個未附加於目前文件樹的元素。 要將元素加入文件,請使用 InsertAdjacentElement or AppendChild 方法。
此 WebBrowser 方法不會影響使用控制項的「 檢視原始碼 」右鍵選單指令或 DocumentText 控制項的 and DocumentStream 屬性 WebBrowser 時,現有文件原始碼的狀態。
當你用 建立新 CreateElement元素時,你將無法設定某些屬性,例如 Name。 如果你需要設定 Name 屬性,可以將它們作為 HTML 指派到 InnerHtml 文件中另一個物件的屬性。