Get the Lowdown on Cache

Also, learn how to display modal data-entry dialogs in an ASP.NET app, and why ASP.NET limits the size of file uploads.





Get the Lowdown on Cache

Also, learn how to display modal data-entry dialogs in an ASP.NET app, and why ASP.NET limits the size of file uploads.


By Jeff Prosise


Q: I'm using ASP.NET application cache to cache DataSets containing database query results. I'm also using cache removal callbacks to refresh the cache when ASP.NET removes a DataSet from it. I store a reference obtained from HttpContext.Cache in a static field so I'm able to access the cache during a cache removal callback. However, the results seem random. Sometimes calling Cache.Insert using the stored reference works, and sometimes it doesn't. What am I doing wrong? Is it not possible to call Cache.Insert from a cache removal callback?


A: Cache.Insert can be called anywhere, anytime, but when you call it during a cache removal callback, it's important to use a reference obtained from HttpRuntime.Cache rather than HttpContext.Cache. This code works fine in a cache removal callback handler:


HttpRuntime.Cache.Insert ("MyDataSet", ds, ...

    new CacheItemRemovedCallback (MyHandler));


But this code is unreliable:


// In Application_Start or elsewhere

_Cache = HttpContext.Current.Cache;


// In a cache removal callback handler

_Cache.Insert ("MyDataSet", ds, ...

    new CacheItemRemovedCallback (MyHandler));


Another nuance of the application cache is that CacheDependency objects passed in Cache.Insert's third parameter can't be recycled. If you use cache dependencies, be sure to create a new CacheDependency object each time you call Cache.Insert.


Q: How can I display modal data-entry dialogs in an ASP.NET application?


A: Modal dialogs can be displayed in Internet Explorer 4.0 and higher by calling window.showModalDialog from client-side script. Displaying a modal dialog is easy enough, but there's a bit of trickery involved in closing the dialog in response to a button click and preventing a postback in the dialog from opening a new browser window.


I've included three files that demonstrate one way to implement modal dialogs in ASP.NET apps (see Figures 1, 2, and 3). To see them in action, copy all three to a virtual directory on your Web server, then open ModalDialog.aspx in your browser and click on the Log In button. In response, a modal dialog appears (see Figure 4). Type a name into the User Name box and type "imbatman"(lowercase) into the Password box. Finish up by clicking on the dialog's Log In button. The dialog disappears and the name you entered replaces the Log In button in the main browser window.






Figure 1. ModalDialog.aspx contains a button that pops up a modal login dialog. Once the user has logged in, a personalized greeting replaces the button.





















User Name

             RunAt="server" />


            TextMode="Password" RunAt="server" />

            RunAt="server" />




Figure 2. Login.aspx provides the login form that appears in the modal dialog.