我有一个带有一系列隐藏输入字段的asp.net网页,我使用它在提交时将值从客户端传递到代码。
<input type="hidden" id="zorro1" value="somevalue set at runtime from client-side" />
<input type="hidden" id="zorro2" value="somevalue set at runtime from client-side" />
.../...
<input type="hidden" id="zorron" value="somevalue set at runtime from client-side" />现在我需要从代码隐藏中提取这些值。我可以写这个丑陋的东西:
dim aValue as string = zorro1.value
dim aValue as string = zorro2.value
.../...
dim aValue as string = zorron.value它可以工作,但我想像这样用LINQ在伪代码中“查找控制”每个隐藏的输入:
dim inputControls = from c in page.controls where id.startswith("zorro") select s
for each ic in inputControls
aValue = ic.value
aId = ic.ID
next谁能把我带到正确的方向?
发布于 2011-09-30 23:25:08
在网上的某个地方找到了这个答案,它是有效的:
在HTML页面本身中,您可以在方便的时候添加对象,例如:
<input type="hidden" id="someMeaningfulID" runat="server" value="some Value" />在Javascript中,很容易改变这些对象的值。在您的后台代码中,添加以下sub:
Private Sub AddControls(ByVal page As ControlCollection, ByVal controlList As ArrayList)
For Each c As Control In page
If c.ID IsNot Nothing Then
controlList.Add(c)
End If
' A pinch of recursivity never hurts :-)
If c.HasControls() Then
call AddControls(c.Controls, controlList)
End If
Next
End Sub当你需要它的时候:
Dim controlList As New ArrayList()
Call AddControls(Page.Controls, controlList)
For Each c In controlList
If c.id.startswith("something I'm looking for") Then ...
If c.value <> "" Then....
If c.someProperty = someValue tThen...
.../...https://stackoverflow.com/questions/7602402
复制相似问题