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!)

Cheesus Toast
  • 1,043
  • 2
  • 15
  • 21

2 Answers2

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?

Community
  • 1
  • 1
Manish Mishra
  • 12,163
  • 5
  • 35
  • 59
  • He's not looking for the JavaScript solution. He is looking for equivalent in VB. – MikeSmithDev Mar 13 '13 at 15:25
  • Thank you for the answer but like mike said I am looking for this type of programming in VB.NET – Cheesus Toast Mar 13 '13 at 15:28
  • Your very last line appears to be what is being asked for. You should expand it. – Brian Nickel Mar 13 '13 at 15:33
  • I think this answer must have been edited or else I have just completely not seen that last bit of the post! I will look into that because that looks like it might be what I am after. I will test it. – Cheesus Toast Mar 13 '13 at 15:39
  • @Manish Mishra That is brilliant Manish Mishra... I have taken the basic idea of what you have done there and I have got it to work! I was beginning to think that I would have to apply a completely different approach. Thank you. :) I can't give you a 1 up because my rep is too low unfortunately. – Cheesus Toast Mar 13 '13 at 16:04
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

Sangeeta
  • 388
  • 5
  • 19
  • It is easy enough to do in javascript actually, I already know how to use it in javascript and vbscript. I have to use VB.NET. It is not an option to use Javascript because it is a university project. I may have to find another method to do what I want to do but I need to explore all options. – Cheesus Toast Mar 13 '13 at 15:35