HtmlDocument.CreateElement(String) Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Cria um novo HtmlElement do tipo de etiqueta HTML especificado.
public:
System::Windows::Forms::HtmlElement ^ CreateElement(System::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
Parâmetros
- elementTag
- String
O nome do elemento HTML a criar.
Devoluções
Um novo elemento do tipo de etiqueta especificado.
Exemplos
O seguinte exemplo de código utiliza dados da base de dados Northwind para criar uma tabela HTML usando CreateElement. O AppendChild método também é utilizado, primeiro para adicionar células (TD elementos) às linhas (TR elementos), depois para adicionar linhas à tabela e, finalmente, para anexar a tabela ao final do documento atual. O exemplo de código exige que a sua aplicação tenha um WebBrowser controlo chamado 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
Observações
elementTag pode ser qualquer uma das tags HTML suportadas em Internet Explorer, exceto FRAME e IFRAME.
CreateElement devolve um elemento não associado à árvore de documentos atual. Para adicionar o elemento ao documento, use os InsertAdjacentElement métodos ou AppendChild .
Este método não afetará o estado do código-fonte de um documento existente quando usar o WebBrowser comando do menu contextual View Source do controlo ou as DocumentText propriedades e DocumentStream do WebBrowser controlo.
Quando cria novos elementos com CreateElement, não poderá definir certas propriedades, como Name. Nos casos em que precisas de definir o atributo Nome, atribui-os como HTML à InnerHtml propriedade de outro objeto no documento.