Tech Off Thread

10 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

LoadControl in Global.asax

Back to Forum: Tech Off
  • User profile image
    W3bbo

    'lo

    I've been looking for a way to programattically load and instantiate UserControls in Global.asax so I can cache them in the Application[] store.

    I had a look with Reflector, it seems LoadControl() is a method that Page derives from TemplateControl, which is an abstract (MustInherit) class.

    So here's a dirty hack I created, can someone confirm if this is the 'right' way to do it?

    namespace W3b.AMS {

    public class Global : System.Web.HttpApplication {

    public Global() {
    }//ctor

    protected void Application_Start(Object sender, EventArgs e){
               
    // Load all the templates           
    ControlLoad TC = new ControlLoad();
    Application["SkinPage"]      = TC.LoadControl(ConfigurationSettings.AppSettings["SkinPage"]);
    Application["SkinContainer"] = TC.LoadControl(ConfigurationSettings.AppSettings["SkinContainer"]);
    Application["SkinArticle"]   = TC.LoadControl(ConfigurationSettings.AppSettings["SkinArticle"]);
    Application["SkinListsDir"]  = ConfigurationSettings.AppSettings["SkinListsDir"];

    }//void

    private class ControlLoad : System.Web.UI.TemplateControl {
               
    }//class

    }//class

    }//namespace

  • User profile image
    borosen

    I have no idea if this is the right way to do it, as far as I know, the TemplateControl.LoadControl is using a caching mechanism anyway so you might not gain much on this.

    But your post have given me input on another problem I have regarding attributes and the new ASP.Net compilation model.

    Thanks.

  • User profile image
    Charles

    Why do you want to do this like this? Your answer to this question will better equip people with the ability to offer better solutions and help you with your understanding of OO programming in the .NET world (since we will be clear about why you want to do what you want to do).

    C

  • User profile image
    W3bbo

    Charles wrote:

    Why do you want to do this like this? Your answer to this question will better equip people with the ability to offer better solutions and help you with your understanding of OO programming in the .NET world (since we will be clear about why you want to do what you want to do).

    Well, I thought that loading a User Control and storing the instantiated control in the Cache would save on HDD access on every page load (since I assumed everytime LoadControl() is used that ASP.NET fetches the file from disk). (Note: I am not confusing this with "PartialCachingControl")

    But after examining the code a little bit more with Reflector, and testing it out, it seems that ASP.NET caches all UserControls in RAM and uses the path to the control as a list indexer (equivalent of a hashtable's hash-lookup?), it only re-loads the control if the file has been modified.

    This is pretty nifty, I'll agree, but the documentation makes no reference to ASP.NET storing the compiled UserControl in memory.

    So anyway, now I just call LoadControl() from the actual page itself now.

  • User profile image
    blowdart

    W3bbo wrote:

    This is pretty nifty, I'll agree, but the documentation makes no reference to ASP.NET storing the compiled UserControl in memory.



    Yea but the documentation doesn't say it loads them from disk every time either. Amusing how you assumed the worst Smiley

  • User profile image
    footballism

    W3bbo wrote:

    But after examining the code a little bit more with Reflector, and testing it out, it seems that ASP.NET caches all UserControls in RAM and uses the path to the control as a list indexer (equivalent of a hashtable's hash-lookup?), it only re-loads the control if the file has been modified.

    Nope, ASP.NET runtime only caches those user controls with @OuputCache directive specified, for more infomation on this, please refer to MSDN documentation.

    Sheva

  • User profile image
    W3bbo

    footballism wrote:
    Nope, ASP.NET runtime only caches those user controls with @OuputCache directive specified, for more infomation on this, please refer to MSDN documentation.


    Read my reply, I explicity stated that I wasn't confusing this with @OutputCache.

  • User profile image
    footballism

    W3bbo wrote:
    footballism wrote:Nope, ASP.NET runtime only caches those user controls with @OuputCache directive specified, for more infomation on this, please refer to MSDN documentation.


    Read my reply, I explicity stated that I wasn't confusing this with @OutputCache.

    But, actually you do get confused with this.

    Sheva

  • User profile image
    W3bbo

    footballism wrote:
    But, actually you do get confused with this.


    Honestly, I'm not Smiley

    When any ASP.NET .aspx or .ascx page is compiled, it inherits its base class and is compiled to a temporary assembly under "\Temporary ASP.NET Files\" in "%windir%\Microsoft.NET\%.NETVer%" and is cached in the computer's memory.

    Evidently, the LoadControl's string "path" parameter is used as a lookup to see if the class is already loaded. I'm not confusing this with ASP.NET's built-in caching, I'm not using the terms interchangably either.

  • User profile image
    footballism

    W3bbo wrote:
    footballism wrote:But, actually you do get confused with this.


    Honestly, I'm not Smiley

    When any ASP.NET .aspx or .ascx page is compiled, it inherits its base class and is compiled to a temporary assembly under "\Temporary ASP.NET Files\" in "%windir%\Microsoft.NET\%.NETVer%" and is cached in the computer's memory.

       Gotcha, it is the dynamically compiled assembly that is cached by ASP.NET runtime when the .aspx pages or .aspx pages containing user controls is first requested, In actuality, the Page instances or UserControl instances are instantiated every time the pages are requested. but if you take the advantage of fragment caching, you have the ability to cache the static portions of the page, mostly those static contents can be wrapped in user controls. when you use LoadControl method to dynamically load a user control, the ASP.NET runtime first checks to see if the UserControl type is marked with the custom attribute PartialCacheAttribute, if so, the runtime will instantiate a PartialCacheControl instead of a normal UserControl instance. the PartialCacheControl have a mechanism to check if the specified UserControl instance has any item stored in the standard ASP.NET cache, if so, it will just return the instance stored in the cache, and add it to the containing page's control tree, if not, it will  dynamically create a UserControl instance, and add it to the cache.

    All in all, cached assemblies and cached control instances are two quite different things.

    Sheva

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.