I was working on an application today, and I needed to add some data to every HyperLink on the ASP.NET page (for a custom authorization string). I thought it might be a common thing: needing to go through all the controls on a page, but apparently not. I didn't find any framework functionality, and the only code samples (just to see if I have the “best” way of doing things) led to some not-so-nice code (arraylists and recursion!). So, here's the best I've come up with (criticism, please):
Stack<Control> remainingControls = new Stack<Control>(); remainingControls.Push(this); do { Control currentControl = remainingControls.Pop(); foreach (Control item in currentControl.Controls) { if (item is HyperLink) { HyperLink hl = (HyperLink)item; hl.NavigateUrl = AddAuthToUrl(hl.NavigateUrl); } else if (item.Controls.Count > 0) { remainingControls.Push(item); } } } while (remainingControls.Count != 0);
Remember Me