Share via


HtmlDocument.GetElementById(String) Método

Definição

Recupera um único HtmlElement usando o atributo ID do elemento como um termo de pesquisa.

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

Parâmetros

id
String

O atributo de ID do elemento a ser recuperado.

Retornos

Retorna o primeiro objeto com o mesmo atributo ID como o valor especificado, ou null, caso o id não possa ser encontrado.

Exemplos

O exemplo de código a seguir recupera um nomeado TABLE de um documento, conta o número de linhas e exibe o resultado na página da Web. O exemplo de código requer que você tenha um WebBrowser controle em seu projeto chamado WebBrowser1e que você carregou uma página da Web com um TABLE cujo ID atributo é Table1.

private Int32 GetTableRowCount(string tableID)
{
    Int32 count = 0;

    if (webBrowser1.Document != null)
    {
        HtmlElement tableElem = webBrowser1.Document.GetElementById(tableID);
        if (tableElem != null)
        {
            foreach (HtmlElement rowElem in tableElem.GetElementsByTagName("TR"))
            {
                count++;
            }
        }
        else
        {
            throw(new ArgumentException("No TABLE with an ID of " + tableID + " exists."));
        }
    }

    return(count);
}
Private Function GetTableRowCount(ByVal TableID As String) As Integer
    Dim Count As Integer = 0

    If (WebBrowser1.Document IsNot Nothing) Then

        Dim TableElem As HtmlElement = WebBrowser1.Document.GetElementById(TableID)
        If (TableElem IsNot Nothing) Then
            For Each RowElem As HtmlElement In TableElem.GetElementsByTagName("TR")
                Count = Count + 1
            Next
        Else
            Throw (New ArgumentException("No TABLE with an ID of " & TableID & " exists."))
        End If
    End If
    GetTableRowCount = Count
End Function

Comentários

Se houver vários elementos no documento com o mesmo valor de ID, GetElementById retornará o primeiro que encontrar.

Aplica-se a

Confira também