HtmlDocument.GetElementById(String) 메서드

정의

요소의 ID 특성을 검색 키로 사용하여 단일 HtmlElement를 검색합니다.

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

매개 변수

id
String

검색할 요소의 ID 특성입니다.

반환

HtmlElement

지정된 값과 동일한 ID 특성을 갖는 첫 번째 개체를 반환하거나, id를 찾을 수 없는 경우 null을 반환합니다.

예제

다음 코드 예제에서는 명명 된 검색 TABLE 문서에서 행의 수를 계산 하 고 웹 페이지에 결과 표시 합니다. 코드 예제에서는 있어야를 WebBrowser 라는 프로젝트에서 컨트롤 WebBrowser1를 사용 하 여 웹 페이지를 로드 하 고는 TABLEID 특성은 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

설명

동일한 ID 값을 사용 하 여 문서의 여러 요소가 있으면 GetElementById 찾은 첫 번째 반환 됩니다.

적용 대상

추가 정보