1

Is there any way in which you can access the properties of an html element by breaking up the id into a string in ASP.NET? By this I am referring to the way in which you can do this with JavaScript to cycle through a numbered set of IDs like an array.

For example I can treat Div1 to Div10 like an array by going:

document.getElementByID("Div" + i).innerhtml  

I cannot seem to find any information on this or a way of asking it in a google search.

If this type of thing is not possible then I would be interested to hear some examples of how else this could be done. I am surprised I cannot find an answer because I am sure many people use the type of method I mentioned in JavaScript.

UPDATE: Please give me a while to give an example of code. I thought this was self explanatory but seemingly it is not... This is VB by the way.

Someone put javascript as a tag!! This is asp.net VB.

Is this basic example enough?

sub exampleProcedure()
    For i = 1 to 10
        document.getelementbyid("Div" & i).innerHTML = "A String"
        if i = 5 then
            document.getelementbyid("Div" & i).innerHTML = "This is Div5"
        end if
    next
end sub

Oh and it might be worth mentioning that that would work in VbScript (which I have been unfortunate enough to have had to work with at university!)

4

2 回答 2

2

if you want to iterate through id. then firstly, your html elements should be runat="server" and then you can do any of these two:

  • you can iterate through the main form controls with a if condition on Id inside
  • you can do .FindControl("ControlId") in a loop of Controls of the main container i.e form

something like :

   For i As Integer = 0 To 5
        Dim control As HtmlGenericControl
        control = Me.FindControl("div" + i.ToString())
        If (Not (control Is Nothing)) Then
            'your logic
        End If
    Next

but this loop will see only the top level controls, i.e. controls which are directly present in the page. i.e. divs inside another divs won't b found.

you will need a recursive loop for that. see this reference. it should give you an idea.

am a C# dev, and have no clue of VB. but i guess you got the idea right?

于 2013-03-13T15:11:17.720 回答
0

i think you want to read html inside div having ID prefix as "Div"

You can do that using jquery easily:

for(var i=0;i<10;i++)
$('Div'+i).html();//this will give you all html within Div0,Div1....Div9

Hope this helps

于 2013-03-13T15:27:13.810 回答