Always Get Better

Archive for May, 2008

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:

protected void gvMessageList_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (e.Row.RowType == DataControlRowType.DataRow)
{
row.Cells[2].Text = row.Cells[2].Text.Replace("\n", "<br />");
}
}

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

Take Yourself Less Seriously, Manage Scope Creep

Tuesday, May 13th, 2008

Given the oportunity, your boss and your client would add so many requirements to your project that your only hope to meet your deadline was to forsake your personal life and donate your evenings and weekends to your job.

This is uncool.  Unless you’re in Houstin trying to bring home Jim Lovell, no one’s life but yours hangs in the balance of your work.  I urge everyone to ask themselves how important, really, is their function.  We tend ot build up walls around us and believe that our problems are big deals, but the reality is that in ten years we won’t remember what we did at work today.

So next time you’re asked to add “one more feature” to your prototype, decide if it’s best for the project, and if it’s possible for you.  It can be hard to say no, but humans weren’t intended to sit in front of the computer programming meaningless crap all day.  Go out and enjoy your life!

Blow Out the Candles for Spam’s Birthday

Saturday, May 3rd, 2008

Kudos to this article for acknowledging MUDs as the source of the term “SPAM”: A very unhappy birthday to spam, age 30

Now that spam has been with us for 30 memorable years, let’s take a moment to think of all the great things spam does for us:

  1. Enlarged lower members
  2. Enhanced “motivational” drive
  3. Pleased partners
  4. Cheap pharmaceuticals (like aspirin, of course)
  5. Lonely ladies with web cams
  6. University degrees for the masses
  7. Winning notifications for lotteries we didn’t even know we entered
  8. The opportunity to help the families of political victims retrieve their relative’s wealth, and share it with us
  9. Quality Rolex watches at amazing discounts
  10. Escape from debt

Not to mention it has become Nigeria’s chief export.

“Connection to Server Reset” when Installing Drupal

Friday, May 2nd, 2008

Has anyone else had this issue?

When I try to install Drupal on a Windows 2003 Apache server, I get a pause and then “Connection Reset” error under Firefox.  If I then try to install it using Internet Explorer, the installation process comes up immediately and works without a hitch.

I still can’t seem to get the admin ‘Modules’ page to load at all – PHP is crashing.  That is an entirely separate issue as far as I can tell.

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