Search code examples
c#.netasp.netfindcontrol

Have I to cache FindControl() result if I use it a number of times?


Does FindControl() work quick or not?

Have I to cache a result using a property like this or not if I search and use the same control a number of time?

private MyUserControl c;
private MyUserControl MyC
{
    get
    {
        if(c == null)
            c = (MyUserControl)FindControl("c");
        return c;
    }
}

Solution

  • If you're talking across requests then don't. You can't in fact. Control references only exist temporarily while the page is rende Putting them in session or some other persistent cache let's them persist and screws up the garbage collector