HtmlDocument.GetElementById(String) Méthode

Définition

Récupère un single HtmlElement à l’aide de l’attribut de l’élément ID comme clé de recherche.

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

Paramètres

id
String

ID de l'attribut de l'élément à récupérer.

Retours

Retourne le premier objet avec le même ID attribut que la valeur spécifiée, ou null si l’objet id est introuvable.

Exemples

L’exemple de code suivant récupère un nommé TABLE à partir d’un document, compte le nombre de lignes et affiche le résultat dans la page Web. L’exemple de code exige que vous disposiez d’un WebBrowser contrôle dans votre projet nommé WebBrowser1et que vous ayez chargé une page web avec un TABLE dont ID l’attribut est 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

Remarques

S’il existe plusieurs éléments dans le document avec la même valeur d’ID, GetElementById retourne le premier qu’il trouve.

S’applique à

Voir aussi