2011-09-29 83 views
0

我有一个asp.net网页,其中包含一系列隐藏的输入字段,用于在提交时将值从客户端传递到代码背景。从代码隐藏遍历DOM

<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 

它的工作原理,但我想“的FindControl”每个隐藏输入这样的,使用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 

能有人把我在正确的方向?

回答

0

在网上找到了这个答案的地方,和它的作品:

中的HTML页面本身,您可以在您方便的添加对象,如:

<input type="hidden" id="someMeaningfulID" runat="server" value="some Value" /> 

其在JavaScript中容易改变这些对象的价值。 在后面的代码,添加此子:

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... 
.../...