in

Telligenti

Serving up fresh ideas every day, Telligent style

Jay Leask's Blog

April 2007 - Posts

  • Controling Repeaters

    I hate finding controls in repeaters, there is no easy way … it’s sloppy, ugly and prone to errors. Here’s how I’ve done it in the past

    Assume:

    • Repeater control “rptrMyDocumentComments”
    • Control location is within the reapeater header (or footer)
      • Header: rptrMyDocumentComments.Controls[0]
      • Footer: rptrMyDocumentComments.Controls[rptrMyDocumentCommentsControls.Length-1]
      • Misc rows: good luck

    Code:

    ASPX.CS file

    if ((rptrMyDocumentComments != null) && (rptrMyDocumentComments.Controls[0] != null)
    && (rptrMyDocumentComments.Controls[0].FindControl("tbLCMyDocumentNumber") != null)) { HtmlContainerControl spanMyDocumentNumber =
    (HtmlContainerControl)rptrMyDocumentComments.Controls[0].FindControl("tbLCMyDocumentNumber"); spanMyDocumentNumber.InnerHtml += sMyDocumentNumber; }

    In a small bitching session to my mentor (who I'm calling out for not blogging regularly enough recently!) I got told "That's to much work to find a control" and pointed me towards a recursive function by Jeff Atwood who was kind enough to blog about this very problem.

    It may mean more code for this one time, but a) cuts out the guessing game and b) is reusable EVERYWHERE you may need to access controls within a repeater (which in my current project is far too often)

    Assume:

    • Nothing

    Code:

    ASPX.CS file
    HtmlContainerControl spanMyDocumentNumber =
      (HtmlContainerControl)MyNamespace.Common.Controls.FindControlRecursive(rptrMyDocumentComments, "tbLCMyDocumentNumber");
    if (spanMyDocumentNumber != null) {   spanMyDocumentNumber.InnerHtml += sMyDocumentNumber; }
    MyNamespace.Common file
    public static class Controls { public static Control FindControlRecursive(Control root, String id) { if (root.ID == id) { return root; } foreach (Control c in root.Controls) { Control t = FindControlRecursive(c, id); if (t != null) { return t; } } // foreach control in root return null; } // method FindControlRecursive } // class Controls
    Many thanks to Tim Rayburn for the suggestion, and Jeff Attwood for being smart enough to come up with the code I'm so delightfully stealing!
    Posted Apr 27 2007, 03:42 PM by Digital Serenity
    Filed under: ,
  • Why the Blog

     I was asked what I wanted to do with my blog here, and it got me thinking, I should really try to set a focus here, no one is going to want to read EVERY random thing on my mind are they?

    Too bad - heh. For the most part my focus will stay Technological, however, it won't always be that way. Every so often I'll cross post one of my hockey posts or I'll go off on politics or some other pop-culture thing that is getting on my nerves. I'll even share things about my family and friends; but mostly I'll talk technology.

    Why? Because it's what I do, it's what pays the bills, and it interests me a great deal. Plus, I hear having these blog things can help your career!

    So thanks for coming by, thanks for reading, but mostly, thanks for putting up with my Chaotic Ramblings!

     

Powered by Community Server (Commercial Edition), by Telligent Systems