Ben Holden Crowther
Ben Holden Crowther

Reputation: 109

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.

e.g. <input id="123" name="**123**">

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

Thanks

Upvotes: 1

Views: 26396

Answers (2)

Don Thomas Boyle
Don Thomas Boyle

Reputation: 3045

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

Upvotes: 2

ORION
ORION

Reputation: 2411

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.

Upvotes: 0

Related Questions