
In my previous entry I created an application using C# in .NET, using windows forms. While very basic, it performs its task well. Has a ‘tweet length’ that updates when text is entered, and uses a timer to update the text of the tweet to reflect the latest song being played from your Spinitron playlist. Unfortunately, the radio station’s computers use OpenSUSE, so even my target audience (while a small one), can’t really use it without bringing in a windows machine.
All the cool kids are always talking about ASP.NET so I figured I’d take a look. I have a background in C/C++. My day job has kept me busy in a very legacy codebase for the past 10+ years. I have broadened my skill set over the years to include things like Cocoa,MFC, and .NET (windows forms). I am no Vostokov but I can hold my own in a .DMP file, and Windbg is DEFINITELY my favorite debugger. Bottom line is, I feel prepared. However, I wouldn’t know a postback from a style sheet. But, how hard can it be?
I decided to start by just making a web application of SpiniTweet, the radio station twitter application from my previous post. This way people at the station could use it anywhere, and it would also give me a good intro to ASP.NET. One of the books I owned had a chapter on web forms, so I decided to start there.
Turns out, it seemed one of the principals in designing web forms was to make them similar to windows forms. In fact, I was delighted to find out I could reuse my windows form code! So I quickly just banged out a simple web application version.
Launched VS2008, and SpiniTweet4Web was born! Before I touched the code, I went into the designer and quickly made a similar form. It seemed clunky, after using web forms for so long, but I noticed that it was making the html page as I added controls, so I figured it felt that way cause it was following all the rules of how a web page is arranged. I got the basics done, leaving out the countdown timer, and the tweet length for now:

Ok, so its not pretty, but whatever, I am just learning, and I can deal with that later. After getting that arranged, I took a look in the code. It seemed to make sense. Page_Load() is the same as the Form1() constructor. So I just added my initialization code there. Double-clicking the button allowed me to have a _click() handler. Fantastic! It all makes sense!
First problem I ran across is understanding the postback functionality. For example, in Page_Load(), I called ‘UpdateFromSpinitron()’ which goes and grabs the latest song played on WMFO, and formats a nice string to be tweeted. If I modify the text and click ‘Tweet This’, you’d think it would just tweet whatever was in the text box, since I query the tweetMsg text box in the button handler here:
System.Net.ServicePointManager.Expect100Continue = false;HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(TwitterJsonUrl);
string post = string.Empty;using (TextWriter writer = new StringWriter()){ String tweetMsgStr = tweetMsg.Text;
writer.Write("status={0}", HttpUtility.UrlEncode(tweetMsgStr)); post = writer.ToString(); Console.WriteLine("Post: {0}", post);}
Not so! Each time it does a postback, it calls Page_Load() again, which calls ‘UpdateFromSpinitron’ overwriting whatever I had changed in the textbox. This was the first, of many, similar but different experiences with ASP.NET. Further, this postback reinititalizing the page seems to be a common theme throughout various controls. Seems like a common issue. You get around it by only initializing if it isn’t a postback like so:
protected void Page_Load(object sender, EventArgs e){ if (!this.IsPostBack) { UpdateFromSpinitron(); } AddCharCount();}
Easy enough. Now, how do I make a twitterish tweet length counter? This is where stuff started to get difficult. Getting the feedback on a per keyup seems to be very difficult on the server side. You can do ‘Autopostbacks’, which automatically send a postback anytime the control is updated, but only when you de-focus the control is it updated, and thus posted back to the server. Further, I came to the conclusion that you really don’t WANT it to be posting back after every keyup. So what else?
Enter AJAX. What the who? Oh yeah, I think I heard about that. Google, right? Talk about behind the curve. This is what living in a legacy codebase for 10 years will do to you.
So I picked up a couple of the biggest books on ASP.NET 3.5 I could find, both had chapters on AJAX. That taught me the basics of how the implementation works. But I wasn’t able to really get my length counter working until I found this webpage. It is a fantastic tutorial talking about exactly what I needed. Apparently you can’t get away from learning javascript!
So there you have it…my first attempt at ASP.NET. You can see the complete code for my website at codeplex. And even check it out running here: www.pj4533.com/spinitweet
Just a comment, I was hoping that ASP.NET would allow an identical workflow as working with windows forms. It does look like they are pushing in that direction, but they just aren’t quite there yet. This has been exciting to learn, and I plan to write many more web apps now! I also plan to add features to SpiniTweet such as a more sexy look (CSS pages), facebook integration, and URL shortening. I’ll post up some blog entries when I do so!