开发者

traversing DOM from code-behind

开发者 https://www.devze.com 2023-04-10 02:28 出处:网络
I have a asp.net webpage with a series of hidden input fields that开发者_运维问答 I use in order to communicate values from client-side to code-behing at submit time.

I have a asp.net webpage with a series of hidden input fields that开发者_运维问答 I use in order to communicate values from client-side to code-behing at submit time.

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

Now I need to extract these values from code-behind. I can write this ugly thingy:

dim aValue as string = zorro1.value
dim aValue as string = zorro2.value
.../...
dim aValue as string = zorron.value

It works, but I would like to "findcontrol" each hidden input like this, with LINQ, in pseudo-code:

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

Can someone put me in the right direction?


Found this answer somewhere on the web, and it works:

Within the HTML page itself, you can add objects at your convenience, like:

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

From Javascript its easy to change the value of such objects. In your code behind, add this 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

And when you need it:

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

精彩评论

暂无评论...
验证码 换一张
取 消