HtmlDocument.GetElementById(String) Método

Definição

Recupera um único HtmlElement usando o atributo ID do elemento como 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 recuperar.

Devoluções

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

Exemplos

O exemplo de código seguinte recupera um nome TABLE de um documento, conta o número de linhas e apresenta o resultado na página Web. O exemplo de código exige que tenha um WebBrowser controlo no seu projeto chamado WebBrowser1, e que tenha carregado uma página 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

Observações

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

Aplica-se a

Ver também