Posts Tagged ‘C#’

CommandEventHandler Event Won’t Fire for Button in ASP.NET Custom Control

Wednesday, September 24th, 2008

Problem: I created a custom control with a dynamic button and attached an event handler to that button.  When I run the control, clicking the button causes a postback but the event is not fired.

Solution: Changed the class inheritance from WebControl to CompositeControl.  Re-compiled and it worked like a charm.

What I Learned from MUDs

Friday, August 1st, 2008

The first bit of code I ever worked on was ROM 2.4, a text-based MUD server written in C.  At the time I didn’t know the first thing about programming, but through trial and error I was able to feel my way around the source code and slowly learn how the language worked and how to make the program do what I wanted.  Some experienced programmers called me the “snippet king” because I used a lot of pre-written additions to expand m game, but I see it as part of the learning process.  If I were to work on a MUD again, I now have the knowledge to do all of the programming myself - but if it weren’t for my baby steps back then I may never have become a programmer at all.

Text-based gaming is all but unheard of now that games like World of Warcraft and Everquest have taken hold.  In my opinion this is a crime because the worlds created through words were so detailed and so interactive that replacing them with pretty graphics makes for a much shallower experience.  It used to be possible to make life-long friends and really share interested, now the genre has been opened up to the lowest common denominator and there is so much swearing and name-calling that many older, more mature players don’t even bother.

I learned a lot of skills from my days playing MUDs that have helped me in my professional life:

  • Programming (obviously)
  • How to interact with others online
  • How to type very fast
  • How to write with personality
  • How to be thick-skinned (when people think they are anonymous, they are quicker to criticize and personally attack you, especially when you’re involved in content creation)

Replacing / Adding Line Breaks in GridView Text

Tuesday, May 27th, 2008

The GridView is a powerful control for quickly and easily displaying tables of data. However, a raw dump of information is not always good - when displayed by a web browser, normal line breaks are simply rendered as spaces.

For long blocks of text, it may be desirable to have your GridView insert HTML line breaks into your data. This can be accomplished either programatically or declaratively.

Programatically

As a programmer, my first instinct is to try to solve the problem using code behind. I add a RowDataBound event handler to my GridView and create the command this way:

Although it works, it has several drawbacks:

  • This solution uses a magic number to cause the compiler to replace the third column in the row.  If the structure of the GridView were to change, this function may break
  • This solution requires the developer to be aware of the final layout of the GridView and to make the connection between the control’s declaration and its logical code.

Use The Design

By far, the better solution is to simply declare the formatting changes in the same place as the GridView.  Using a Template field, I can add line breaks to my message by adding this:

<%# Eval(”Message”).ToString().Replace(”\n”, “<br />”) %>

More Information

This solution assumes the contents of “Message” are not null.  For more information about this technique (including how to deal with null values), I recommend the ASP.NET message boards: http://forums.asp.net/p/1027728/1403884.aspx

C#: Using Suffixes to Declare Data Literals

Thursday, May 1st, 2008

This isn’t new, but handy to have.  In order to tell the compiler which data type we’re using, we use suffix notation as follows:

  • Unsigned integer, U: e.g. 34506U
  • Long integer (signed), L: e.g. 5297532L
  • Unsigned long integer, UL: e.g. 30958UL
  • Float, F: e.g. 13.6F
  • Double, D: e.g. 14.3D
  • Decimal, M: e.g. 19.95M

C#: Finding the Number of Days Between Two DateTime Items

Tuesday, April 29th, 2008

One very common requirement is for the number of days elapsed since a particular Date and Time. In C# this can be accomplished through the use of the TimeSpan class.

The easiest way to create a TimeSpan is like this:

Handling Relative Paths Programmatically In ASP.NET

Friday, April 25th, 2008

One of the nicest features in ASP.NET is its out-of-box support for relative paths in hyper links and other controls. This is very important for developers whose code resides within the root of their testing environment but within a sub-directory of the production server.

Whereas one would have to carefully program things like image links to point at “/applicationbasepath/images/”, in ASP.NET we can simply use “~/images”. Genious!

When writing custom code, however, the work isn’t done for us automatically. We have to pass our URL to a server-side function in order to convert the “~/” into a base path usable by our visitors.

There are two ways of doing this:

Using Relative Paths From a Web Page or Control

If we are programming a page template or a server control, we can use the ResolveUrl() function provided by our environment context.

On AlwaysGetBetter.com, “~/index.php” would resolve to http://www.alwaysgetbetter.com/blog/index.php. On another site, it might resolve to the root such as http://www.abetterblog.com/index.php. Programming this way makes our solution more portable.

Using Relative Paths From Outside a Web Page

Sometimes we need to use relative paths from outside the context of a web page. For example, if we were to make a change in our Global.asx file, the program code we use may not be considered to be within a web page scope.

Fortunately, .NET provides us with the static helper class VirtualPathUtility.

C#: Instantiating a class of Unknown Type

Saturday, April 5th, 2008

The project I am currently working on has several dozen different types of Form classes, each of which is accessible from a common menu strip. Rather than repeatedly instantiating each of the forms from the menu item handlers, I wanted to funnel the request to a single function.

The problem is: How do you instantiate a form when the type is unknown?

The code snippet is: