Brendan Enrick

Daily Software Development

Accessing a MasterPage ScriptManager from a Content Page

Recently I had forgotten how to access the ScriptManager in my MasterPage from one of the Content Pages. There is a static method on the ScriptManager class called GetCurrent() which will allow access to the current instance of a ScriptManager. This is useful because the ScriptManagerProxy is really just designed to do the declarative work normally performed on the ASP.NET page, but some work needs to be done through code. An example would be to check the ScriptManager instances IsInAsyncPostback property.

if (ScriptManager.GetCurrent().IsInAsyncPostback)


    // Perform only in asynchronous postback logic here.

This is very useful and easy, but I seem to always forget it is here. Perhaps now that I have blogged about it I will remember, and if not I can at least come back here to find it. Yes, when I forget I go through the trouble of casting Master as my MasterPage's class and then I access it that way (what a pain).

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
        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.

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.