2011-09-29 30 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... 
.../...