如何在.net visual basic中按名称获取元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18622067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get element by name in .net visual basic
提问by Ben Holden Crowther
I know how to get an element by its ID in visual basic (.net) with getelementbyid.
我知道如何使用 getelementbyid 在 Visual Basic (.net) 中通过其 ID 获取元素。
But how do I get an element by its name attribute.
但是如何通过名称属性获取元素。
e.g. <input id="123" name="**123**">
例如 <input id="123" name="**123**">
And if so, how do I interact with said element.
如果是这样,我如何与所述元素进行交互。
Thanks
谢谢
采纳答案by ORION
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).
您可以添加runat="server"到您的元素<input id="123" name="**123**" runat="server">,然后只需通过其 id 服务器端引用该元素,只要您在其中引用您的元素的函数不是共享函数(如果它是共享的,您将需要创建它的类以引用它)。
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.
希望这可以帮助。
回答by Don Thomas Boyle
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

