Page.LoadControl
My last big frustration has been attempting to make it so that my CMS can render templates in the VS.net designer that look like the page being generated. I use a homespun derivitive of the MasterPages setup to do my templating. Unfortunitly, under the VS.net designer any attempt to do a Page.LoadControl(VirtPathToUser) results in a “object reference is null“ exception. (BTW, if you ever want to know how to debug a control in the VS.net designer, open up a second instance of VS.net with the same project, set a break point and then attach to VS.net. Works like a charm). Using a ControlDesigner, I can make it look pretty, but I can't load the template in anyway because it throws that exception. I may be SOL here. I also tried this with MetaBuilders awesome masterpages, and ran into the same problem. (I can not plug Metabuilders enough). Andy Smith has a pretty good blog, but he does not blog often enough ;-). My next step may be to try and manually create the objects this reflection, but I have not figured out how to get the .aspx file rather then the .ASPX.cs class. Anyone run up against this before?
However, there is also my new favorite ASP.net caching trick. I previously used the ASP.net Cache to store my data objects because my pages are either a) static when viewed by outside users or b) very dynamic when used by editors and authors. This is not a optimal solution because even storing your data objects does not give you the best performance boost. Rendering the page still means a complete trip thru the (sometimes expensive) ASP.net pipeline. ASP.net offers page by page caching which gives a even more substantial performance boost, but page caching tends to limit how dynamic pages can be. I looked for a way to programatically invalidate page cache from inside of ASP.net, but there was still the somewhat painful issue of postbacks and dynamic renders and making sure that you are only caching at appropriate moments. Then, I came across the (apparently little known) Response.Cache.AddValidationCallback. This method allows you to do cache validation on a request by request basis. Using this as well as the CachePolicy option it is possible to tell ASP.net to use a stored cache image of a page, ignore the page request (and render) or invalidate the request. You can even change the HTTP cache headers to cache pages longer on client boxen.
For example:
public void Validate(System.Web.HttpContext Context, object State, ref System.Web.HttpValidationStatus Status)
{
if (((VeronaPage) this.Page).Editing)
{
Status = HttpValidationStatus.IgnoreThisRequest;
}
else
{
Status=HttpValidationStatus.Valid;
}
}
You can also set Status to HttpValidationStatus.Invalid if you want to force re-rendering he page. You hook this up by adding the following code to your (local) Page_Load:
Response.Cache.AddValidationCallback(new HttpCacheValidateHandler(this.Validate), null);
finally, this had a interesting note about useing pages other then Page for inheritence of ASP.net Pages:
http://www.msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnaspp/html/aspnet-pageobjectmodel.asp