简体   繁体   中英

How to get element by name in .net visual basic

I know how to get an element by its ID in visual basic (.net) with getelementbyid.

But how do I get an element by its name attribute.

eg <input id="123" name="**123**">

And if so, how do I interact with said element.

Thanks

Again the assumtion lies with you using the webbroswer

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

    'NAVIGATE
    WebBrowser1.Navigate("whereveryourgoing")
    'Waiting for page to load function
    WaitForPageLoad()

    'Get Element by Name
    Dim fb_button As String = String.Empty
    For Each element As HtmlElement In WebBrowser1.Document.All
        If InStr(element.Name, "loginBox") Then
            fb_button = element.Id
            'MsgBox(fb_button & " Found! ")
            'MsgBox(element.InnerHtml.Length.ToString)
            'MsgBox(element.InnerHtml.ToString)
        End If
    Next

    '\/ - Perform Actions
    WebBrowser1.Document.GetElementById(fb_button).SetAttribute("value", Password) 'or InvokeMember("submit") or InvokeMember("click")
    MsgBox("Done")

End Sub

Source

If I understand you correctly, you will need to do something like this:

            Dim myInput As HtmlInputControl = CType(e.Item.FindControl("123"), HtmlInputControl)

Then do stuff with the input you just created

            myInput.Enabled = False

OR

You could just add runat="server" to your element <input id="123" name="**123**" runat="server"> and then just reference the element by its id server side as long as the function you're referencing your element in isn't a shared function (if it's shared, you'll need a new instance of the class it was created in to reference it).

           123.Enabled = False

Also, I'm not sure if you're just doing it to show the example, but you might run into some surprises if you use asterisks in your element names due to it's use as a wildcard character. Generally, you want to keep your attributes with numbers and letters (plus hyphens and underscores) only ;)

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM