Monday, December 21, 2009

Looking back, moving forward (New years post)

In this post I'm going to reflect on the year that's almost finished and the year to come.

I came up with a few topics that are worth discussing in this post.

My career

I don't think I ever mentioned where I work. The first of January I will be working 18 months for Ferranti. The only company I've worked for since I graduated.

The first project I've ever worked on was a rather big one. Firestation Antwerp. Last year we heard that sales scored another firestation for us (Firestation Ghent). So I've been working on these two projects since I started.

Do these projects get boring? Well, not boring, exhausting maybe. These projects are very complex (Dispatching, telecommunication, websites, GIS, ..) and the requirements are very high. Although these projects are exhausting from time to time, I don't think there are other projects where I would've learned as much in such a small period of time. As a developer I have touched and worked on lots of stuff: ASP.NET, WindowsServices, WCF, WebServices, GIS, API's, Telecommunication.. You name it.

Because I've had the chance to play with so many things I'm pretty sure that web development fits me best. So next year I will try to mainly focus on web developement.

Blog

I think I've been blogging since October 2008. When I just started out the quality of the content was rather poor. Although I'm not totally satisfied with the content I produced this year, I hope the quality has improved noticeable. I saw the traffic increase significantly this year, so I hope that's a good sign.

There are a few posts that scored pretty good out there. You can find them here. I'd like to thank Arjan Zuidhof, Lee Dumond, Alvin Ashcraft, .. for helping me getting my blog out there.

By blogging I've made lots of new friends. I enjoy reading through all of your Tweets. Bart De Smet in particular has been a very good mentor.

Community

I managed to go to two events this year:
- TechDays Belgium
- Scott Guthrie in Belgium

I hope I will get the opportunity to go to some big event next year. I also plan on going to more VISUG sessions.

I've been neglecting the ASP.NET forums lately. In general I think the quality of the questions is rather poor and there are plenty of good answerers. I've been a long time StackOverflow lurker. Next year I'll try to contribute to that community.

Travelling

I managed to travel four times this year:

In 2010 I'll try to go abroad at least three times.

Wrap up

In general, my year was pretty fun. I hope 2010 will be at least as good. Excited about hearing how you did this year and what you hope to achieve in 2010!

Thursday, December 17, 2009

Sunday, December 13, 2009

The problem that ViewStateMode solves

A new feature of ASP.NET 4.0 is the ViewStateMode property on a Control.

You can use the ViewStateMode property to enable view state for an individual control even if view state is disabled for the page.
Source:Msdn

In this post I'll try to give this new feature a chance to shine and show it's use.

Problem to solve: Disable ViewState on the Page and enable it on an individual Control.

Let's try to solve this without the ViewStateMode property. Simply disable the ViewState on the page and enable it on lblViewstate1.

   1:  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewState_3_5.aspx.cs" Inherits="Demo_2___ViewState.ViewState_3_5" 
   2:      EnableViewState="False"%>
   3:   
   4:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   5:   
   6:  <html xmlns="http://www.w3.org/1999/xhtml">
   7:  <head runat="server">
   8:      <title></title>
   9:  </head>
  10:  <body>
  11:      <form id="form1" runat="server">
  12:      <div>
  13:          <asp:Panel runat="server" ID="pnViewState">
  14:              <asp:Label runat="server" Text="[Static value]" ID="lblViewState1" EnableViewState="True"/>
  15:              <asp:Label runat="server" Text="[Static value]" ID="lblViewState2"/>        
  16:              <asp:Button runat="server" Text="Do PostBack" ID="btnDoPostBack" onclick="btnDoPostBack_Click" />
  17:          </asp:Panel>
  18:      </div>
  19:      </form>
  20:  </body>
  21:  </html>

   1:  protected void Page_Load(object sender, EventArgs e)
   2:  {
   3:       if (!Page.IsPostBack)
   4:       {
   5:           this.lblViewState1.Text = "[Dynamic value]";
   6:           this.lblViewState2.Text = "[Dynamic value]";
   7:       }
   8:  }
   9:   
  10:  protected void btnDoPostBack_Click(object sender, EventArgs e)
  11:  {
  12:      //Do Postback
  13:  }

Hmmm this doesn't work. When I click the button and do a PostBack the Label lblViewState1 didn't maintain it's state. It displays [Static value] instead of [Dynamic value].

You could solve this problem by enabling the ViewState on the Page and disable the ViewState on all Controls that don't need to maintain their state. But that's pretty silly right?

This is where the ViewStateMode property comes into play. Check out the example below. I disable the ViewStateMode on the Page and I enable the ViewStateMode on lblViewState1.

   1:  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewState_3_5.aspx.cs" Inherits="Demo_2___ViewState.ViewState_3_5" 
   2:      EnableViewState="True" ViewStateMode="Disabled"%>
   3:   
   4:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   5:   
   6:  <html xmlns="http://www.w3.org/1999/xhtml">
   7:  <head runat="server">
   8:      <title></title>
   9:  </head>
  10:  <body>
  11:      <form id="form1" runat="server">
  12:      <div>
  13:          <asp:Panel runat="server" ID="pnViewState">
  14:              <asp:Label runat="server" Text="[Static value]" ID="lblViewState1" ViewStateMode="Enabled"/>
  15:              <asp:Label runat="server" Text="[Static value]" ID="lblViewState2"/>        
  16:              <asp:Button runat="server" Text="Do PostBack" ID="btnDoPostBack" onclick="btnDoPostBack_Click" />
  17:          </asp:Panel>
  18:      </div>
  19:      </form>
  20:  </body>
  21:  </html>

After doing a PostBack lblViewState1 still shows [Dynamic value] and lblViewState2 shows [Static value]. lblViewState1 maintained it's state!

This feature gives us another reason to stop being lazy and use ViewState where it's designed for.

Hope this helps.

Monday, December 7, 2009

Add leading zeros to a number

Todays post is a very small tip.

I saw the question "How to add leading zeros to a number" on the ASP.NET forums countless times before. And often the answers provide solutions that work, but are overkill as well.

The two cleanest methods I know are String.Format and PadLeft.

   1:  Console.WriteLine("Using .ToString()");
   2:  Console.WriteLine(String.Format("{0:0000}", 16));
   3:   
   4:  Console.WriteLine("-------------------");
   5:   
   6:  Console.WriteLine("Using .PadLeft()");
   7:  Console.WriteLine(Convert.ToString(16).PadLeft(4, '0'));

You can see the result here.

//Using .ToString()
//0016
//-------------------
//Using .PadLeft()
//0016

Saturday, December 5, 2009

Response.RedirectPermanent in .NET 3.5 and older

One of the new features in ASP.NET 4.0 is permanently redirecting to a page using Response.RedirectPermanent.

It is common practice in Web applications to move pages and other content around over time, which can lead to an accumulation of stale links in search engines. In ASP.NET, developers have traditionally handled requests to old URLs by using by using the Response.Redirect method to forward a request to the new URL. However, the Redirect method issues an HTTP 302 Found (temporary redirect) response, which results in an extra HTTP round trip when users attempt to access the old URLs.
Source


You can achieve this functionality in ASP.NET 3.5 and older by writing a 301 Moved Permanently Status and a Location Header to the Response stream. This can be found in the HTTP specifications.

Here is an example.


   1:  Response.Clear();
   2:  Response.Status = "301 Moved Permanently";
   3:  Response.AddHeader("Location", "PageOne.aspx");
   4:  Response.End();



You can verify this by using Fiddler.

Scott Guthrie in Belgium #ScottInBelgium

Yesterday Belgium had the honor to welcome Scott Guthrie.

Scott gave three sessions in Kinepolis Brussels (next to the Atomium and the King Baudouin Stadium) on:
  • Visual Studio 2010 and .NET 4.0 Web development
  • Silverlight
  • ASP.NET MVC 2

Due to some personal obligations and traffic jams in Brussels I had to leave after the first session. So you'll only find my impressions of the first session..

Visual Studio 2010 and .NET 4.0 Web development

Scott demoed some of the neatest Visual Studio 2010 features and improvements. I blogged on some of them here. Neat stuff I don't talk about in that post are:

After finishing up the Visual Studio demo, he continued with demoing ASP.NET 4.0 Web development. If you missed this session, I suggest you read this whitepaper!

Conclusion

All by all it was a very interesting session. It was definitely worth driving to Brussels for. Something extra which will make me think of this session everytime I open my closet is the famous Gu red shirt we all received.



If other Belgian bloggers blogged on this event as well please contact me so I can add a link to your post!
Update 1: Katrien blogged on this event as well. She was lucky enough to make it to all sessions. See her post here.
Update 2: Another post by Gill Cleeren on this event.