Is there a way to find the previous and next sibling controls in an ASP.net form from code-behind, similar to findControl()?
Sometimes you don’t want to assign an ID to a control just so you can do a parent().findControl(“ID”) in order to find it. I’m tired of coming up with IDs when all I could do is previousControl() or something (a la jQuery).
This would also be useful in situations where you write a general function in order to address several controls which have a similar layout and don’t want to address them one by one.
Thanks for any suggestions.
For posterity, here is the function I ended up writing. Works very well (tested in a real project):
To be used like this:
and for next control:
The advantage of this solution over that of Atzoya is that, first, you don’t need the original control to have an ID since I do the search based on instance. Second, you have to know that ASP.net generates several Literal controls in order to render your static HTML in between your “real” controls. That’s why I skip them, or you will keep matching junk. Of course the downside of this is you can’t find a control if it’s a Literal. This limitation was not a problem in my use.