HtmlDocument.GetElementById(String) Método

Definição

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

public:
 System::Windows::Forms::HtmlElement ^ GetElementById(System::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 ID do elemento a ser recuperado.

Retornos

Retorna o primeiro objeto com o mesmo ID atributo que o valor especificado ou null se não id for possível encontrar.

Exemplos

O exemplo de código a seguir recupera um nome 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 nomeado WebBrowser1e que você tenha carregado uma página da Web com um TABLE atributo cujo ID é 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 encontrado.

Aplica-se a

Confira também