As I mentioned in my previous blog post about Accessing a property of a base page from a user control, I am going to explain how to access a property on a MasterPage from the content page. One merely has to check the namespace and the class name of the masterpage, which can be found in the code behind file. Just cast the Content Page’s Master as the class of the masterpage file which it uses, and then just access the value. It is really quite simple.
int neededValue = ((MyNameSpace.MyMasterPageClassName)Master).MyProperty;
Using that method you are able to easily access a property of a masterpage file when needed.
Earlier today I was helping someone who was working with a user control. That control was on an ASP.NET page which was inheriting from a base page. From the user control he could not access the properties of the base page. He mentioned that he was getting an error message which said that the property did not exist in the current context.
I showed him that the reason he was having the problem is because the code in the user control came from the page before, and thus he would need to get the properties from there, but he was also going to need to cast the Page as the base Page in order to get to the property.
int myImportantValue = ((MyBasePage)Page).ImportantProperty;
This will retrieve the value from the property of the base page. It is a fairly simple task. Perhaps next I will show how to do a similar task with a masterpage.
I've now added that blog post about Accessing a Property of a MasterPage from a Content Page.
Earlier today I had a situation where I needed to clear the output cache entry of a page. After a quick Google search I turned up Steve Smith’s Article on Removing Page Output Cache Entries.
The article is a short, helpful article which quickly and easily explains how to remove the output cache entry of a page.
private void RemoveButton_Click(object sender, System.EventArgs e)
{
HttpResponse.RemoveOutputCacheItem("/caching/CacheForever.aspx");
}
I hope everyone else finds this to be easy to understand and implement. The above should work as long as you have a page named “CacheForever.aspx” in the folder “caching” at the root of the site.
Happy Caching!
Lately, I’ve noticed a lot of people who are not careful about how they load objects. Managing objects is a fundamentally important part of software development.
Lets say for example I have an integer in the query string, and I need to use this number in a few places on my page. Well it is obviously inefficient and an ugly process to check the query string and parse the value into an integer every time I want to access that number. I could also at the beginning just grab the number, but this would become a problem if I rearranged things. There is also a chance the execution will not require even checking the query string, and then I will have loaded that value for no reason.
This is a very simple and easy way of retrieving an number from a query string.
private int _pageId = 0;
private int PageId
{
get
{
if (_pageId == 0)
{
int myInt;
int.TryParse(Request.QueryString[”pageId”], out myInt);
_pageId = myInt;
}
return _pageId;
}
}
With this I no longer have to parse the query string or have the ugly Request.QueryString all through my code. One nice thing is that once I have successfully loaded a valid pageId it will not reference the query string again. As you can probably see this is also helpful when making a database call or any other time consuming work to get some data.
This is very useful for more complex data structures. When the object being loaded is large we want to avoid loading it if we do not have to and also loading it more than once. This is what we can achieve here. In that case you would compare the value to null instead of 0. This will let you know if it has yet been loaded.
Yesterday, I updated my SimpleCMS plug-in for ASP.NET. It is a free CMS designed to be different from most content management systems. It is not supposed to include tons of features and be some bloated system. It is designed to be an add-on for an existing site.
Check it out. It is free at ASPAlliance.com. This new release uses the RTM builds of the MS AJAX Library and the AJAX Control Toolkit.
If this sounds like something you might be interested in, then check it out. Let me know how you like it and any suggestions you have for it. Just don't ask for it to become like the huge content management systems. There are plenty of those out there give one of those a shot if that is what you are looking for.