Brendan Enrick

Daily Software Development

Accessing Properties of a Base Page from a User Control

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

Clearing Page Output Cache Entries

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!

Simple Lazy Loading

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.

Simple CMS Plug-in for ASP.NET Update Released

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.

Five Things you did not know about me

Like everyone else, I’ve now been tagged into this game. Steve Smith tagged me so now I will tell you five things about me that you may not know.

1. I have lived in one city my entire life. I’ve been living in Kent, Ohio since I was born, and I have yet to move elsewhere. I’ve lived in 3 homes in Kent. I am currently attending Kent State University, so am still not out of Kent. Not sure when I will leave Kent, but I doubt I will be living here for too many more years.

2. I’ve been a world traveler since I was 10 years old. This is when my family started going on trips. Though my travels have been much more extensive than most people of my age, I have not seen nearly everything I wish to in my life. I have been to Venezuela twice, most islands in the Caribbean, Mexico a few times, Canada more times than I can remember, Hawaii once, England twice, France twice, Corsica once, once to Italy, once to Spain, and I’ve probably forgotten something. Sadly I have not yet been to Germany. I wish to see much more though, and hope to see many places in Africa, Asia, and Australia.

3. I, like Steve Smith, am a gamer. As can be seen by the title of my blog I am obviously some type of a gamer. I enjoy playing Dungeons and Dragons. Yes, I am aware that it is the stereotypical nerd game. It is not the only game that I play though. I enjoy making up my own games. These games have their own worlds and own systems for play. I enjoy playing them a great deal. I also enjoy playing card games, board games, and video games. Most of my games are console games, but I also play a lot of PC games. The board games I play are classics as well as lesser known games. One of my favorites since I was a child is called Solarquest.

4. After number 3 this one becomes more obvious. I would like to eventually work in some type of game design. I think it would be great fun. I might be able to get into video game development with help from the degree in Computer Science I am about to earn from Kent State University. I know many people who would like to work on games. This semester I will be taking a class about designing and building game engines. I hope it is a fun class.

5. Most people don’t know that while I think this five things idea is kind of cool. I have this notion that I am merely propagating a chain letter farther along. I don’t know if others have gotten this same feeling, but it would seem to me that this, while interesting, at its roots is an attempt to see how far the chain lett……. the five things blog will go.

So now I must search for bloggers who have yet to list five things or be tagged. Such a cruel fate. Here is a group of people I have yet to see five things from, and whom I know little about. I apologize in advance if these people have been tagged already or do not want to play the game.

Alessandro Gallo ( Garbin )
Joydip Kanjilal
Ian Lipsky
David Walker
Mo Meng

Constant Search Engine Optimization

With the power that search engines seem to have now, it has become difficult to design anything without considering them. Anytime I am trying to find anything I check Google first, and I often use Google to search sites that have their own searches. One reason I see for this is that all sites seem to try to optimize for search engines now.

I spend a lot of my time considering how a search engine will see any piece of code I write. When a large portion of traffic is directly attributed to a search engine, one has to keep it in mind when designing pages. A lot of times I feel that I have spent more time configuring a site to show higher in search results than I have building the site.

I am just assuming that everyone has run into the same situations. We are all trying to find newer and better ways to get our sites a little higher on those search engines. I am certain I am not the only person who spends a few seconds figuring out a way to word a link so it not only makes sense to a user, but will be seen nicely by search engines. Even in blog posts I am careful about my naming now. It is quite a change from what I was doing a decade ago.

Simple CMS v0.9.61020.1 Released. Now using Microsoft AJAX.

Microsoft just released their new AJAX Libraries, so a new version of Simple CMS has now been released which uses it. I am watching for new controls especially in the AJAX Control Toolkit to add to Simple CMS in future versions. You can suggest changes for future versions of Simple CMS and also ask questions about it using the Simple CMS Forum. Let me know what you think.

I hope everyone is keeping up with all the cool features in the MS AJAX. I hope they have some awesome stuff when we get ASP.NET 3.0

My Team Server Headache

So yesterday I was able to spend a long time battling with team server. It seams that source controls likes to keep track of where your workspaces are itself. So on a computer I don’t normally work on, I logged in. I set up work spaces on that machine, because Team Server didn’t specify them for me. I needed to put the code in a public place, so I did. When I went back to my normal machine, I told the server I wanted to get latest. Wow was that a huge mistake.

Five to ten minutes, and a seemingly endless stream of error messages later, team server has moved half of all of my files to another location. It seems that it noticed from the other machine that I had moved my workspaces, so it tried to do the same on my work machine. All hell broke loose when it could not get all of the files moved correctly. As should be quite obvious, Visual Studio cannot build half projects.

The best part of this whole experience was the time it took to move EVERYTHING back to its original location so I could start working again. All I wanted was 1 updated file. Never going to move workspaces again…..

Perhaps someone knows of some cool trick to stop that from happening. If you do I would appreciate knowing as well. Thanks.

Visible Whitespace in Visual Studio

Something quite horrible happened to me today. While I was working on some of my code, I accidentally hit some keys while holding the control key down. Unfortunately for me I did not see the keys I hit. Well now instead of seeing whitespace there were this little dots. I figured there was some checkbox in Visual Studio’s options. I could not find it anywhere, but I did discover a cool hack. In the fonts section, I found it keeps font information for Visible Whitespace. By changing the font color to white, I wouldn’t have to see the dots anymore or so I thought. Whenever I highlighted one of the dots I could see it again.

Now with my frustration building, what can I say I hate dots, I began trying different keyboard shortcuts. I eventually found what turns on and off white space.

The Answer:    Control + E + S

Hopefully no one else must suffer through the horror that is visible white space.

Simple CMS New Release Version 0.9.60908.1

A newer version of Simple CMS has just been released. The only significant change to Simple CMS is that it now supports sessions. This means that if you have a masterpage you are using with Simple CMS, you may now use the Session object in that masterpage file.

How to enable Session State with HttpHandlers
This was an interesting problem, because there isn't a page object in the normal sense of file with aspx as its extension. Normally one can just add enableSessionState = true at the top of the file. In order to achieve this with HttpHandlers you will need to implement System.Web.SessionState.IRequiredSessionState or System.Web.SessionState.IReadOnlySessionState. To do this you simply add one of these as if you are inheriting from a class.

In my case I use an HttpHandlerFactory, so I specified this for the created handler, and not the factory.