Diving into jQuery, Part 1: A Simple Currency Converter – ASP.NET and jQuery

April 13th, 2010 Ben McCormack No comments

A few weeks ago, I picked up a copy of Matthew MacDonald’s Beginning ASP.NET 2.0 in VB 2005 since I needed to learn ASP.NET for work.  Since we’re currently using the 2.0 version of the .NET framework, I decided to go with an older book (five years!!) now and plan to refresh with newer material if/when we switch to .NET 4.0, which was just released yesterday.

It’s amazing how much has happened with web and general computer technologies since this book was published.  At the time, AJAX was still largely just a concept, Windows Vista had not yet been released, and the iPhone was still a unicorn in Steve Jobs’ backyard.  Perhaps the age of this book gives reason to why I was surprised at the how much enthusiasm the author had for not needing to worry about HTML or JavaScript and let the server do all of the work.

To be sure, I can certainly understand why developers, who survived the world of classic ASP and VBScript, were excited when ASP.NET came along, complete with its features of state management, server and client-side validation, and a host of other niceties which were revolutionary for its time.  Besides, when the dust from the browser wars of the late nineties was settling, who in their right mind wanted to write code for the browser?  Eck.

Times have changed, however, and web developers can reasonably expect client browsers to handle JavaScript (which they were supposed to be able to handle since the late nineties).  So when I came across MacDonald’s example of a simple currency converter in chapter 5 of his book, I was little surprised that it was entirely in ASP.NET.  Granted, this a book focused entirely on ASP.NET, but as I worked through the example, the idea of creating even a simple currency converter to run using server-side code seemed so dated.  Why make a full-round trip back to the server when all you need to do is a simple calculation?

I wanted to try using JavaScript to accomplish the same task.  I decided to user jQuery, a popular JavaScript library, to help me complete the task.  You can see a working demo of my jQuery-converted currency converter here.

To get started with jQuery, you need to add a reference to the jQuery source file in the header of either your HTML or ASPX file.  Microsoft hosts jQuery on its content delivery network (cdn), so I simply linked to their most recent version of the file that included visual studio documentation (hence the –vsdoc in the file name).  I also put all of my jQuery and JavaScript code in a Default.js file, so you’ll see that link as well:

 

<head>
  <title>Currency Converter</title>

    <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="scripts/Default.js"  type="text/javascript" ></script>

</head>

The beginning part of my Default.js file contains a lot of code to get the page ready to handle certain events. Everything is contained in the $(document).ready event and I have separate events for the buttons (Convert and ShowGraph) as well the change event on the currency selector. I also included a toggle option to choose to run the page as ASP.NET or jQuery. If ASP.NET is chosen, all of the JavaScript is basically ignored. If jQuery is chosen, the code is executed when the events fire and the even.PreventDefault() command executed. This code will prevent something like a button from submitting form data if that’s its default behavior.

$(document).ready(function() {
  $("#Convert").click(function(event) {
    ShowConversionResult();
    ShowGraph();
    if ($("#jqueryToggle").val() * 1) {
      event.preventDefault();
    }
  });
  $("#Currency").change(function(event) {
    ShowConversionResult();
    ShowGraph();
  });
  $("#ShowGraph").click(function(event) {
    ShowGraph();
    if ($("#jqueryToggle").val() * 1) {
      event.preventDefault();
    }
  });
});

Here is the ShowConversionResult() function.  You’ll see a few things going on here.  This was a good opportunity for me to learn basic dynamic typing using JavaScript and learn some tricks for dealing with numbers and testing conditions.  I also learned a little about CSS styling using jQuery.

function ShowConversionResult() {
  if ($("#jqueryToggle").val() * 1) {
    var oldAmount = $("#US").val() * 1;
    if (isNaN(oldAmount))
      oldAmount = 0;
    if (oldAmount <= 0) {
      $("#Result").css("color", "red").text("Specify a positive number");
    }
    else {
      $("#Result").css("color", "black");
      var newAmount = oldAmount * $("#Currency").val();
      $("#Result").text(oldAmount.toFixed(2) + " U.S. dollars = " + newAmount.toFixed(2) + " " +
            $("#Currency :selected").text());
    }
  }
}

Here’s the ShowGraph() function of my code.  This part was interesting because the ASP.NET example starts with the graph image disabled when the page loads.   When an element is disabled in ASP.NET, no HTML code will be produced for that element unless the control is enabled on a post-back, which is exactly how the ASP.NET application functions.  However, I wanted to solve this problem using jQuery, so I needed to be able to work with the Graph element even if it did not yet exist.

The line of code that says “if ($("#Graph").length != 0) {“ is testing for the existence of the Graph element.  If it exists, it simply changes the source attribute of the element and the new graph is loaded.  If not, it dynamically generates HTML for the graph element and appends it to the “GraphContainer” div tag (which I had to create in order for this to work; it wasn’t part of the original ASP.NET solution).

function ShowGraph() {
  if ($("#jqueryToggle").val() * 1) {
    var src = "pic" + $("#Currency").attr("selectedIndex") + ".png"
    //Test to see if the Graph element exists:
    if ($("#Graph").length != 0) {
      $("#Graph").attr("src", src)
    }
    else {
      var html = "<img src=\"" + src + "\" alt=\"Currency Graph\" id=\"Graph\" />"
      $("#GraphContainer").append(html)
    }
  }
}

Overall, this was a fun project to learn some of the basics of jQuery while at the same time learning ASP.NET.  JavaScript can make for a richer user experience for the client and jQuery certainly makes for a better experience for the developer. My next post will show how to hack an existing page using jQuery to make images load dynamically instead of making a full trip to the server.

Categories: General Tags:

Pay Me! Easily Accept Money on Your Website

April 3rd, 2010 Ben McCormack 1 comment

I’ve been playing around with PayPal to try to learn ways that people can send other people money over the internet.  For my own personal projects, I have zero interest in learning credit card handling myself, so PayPal makes it easy enough to transfer money from one individual to another (albeit for a small fee).

PayPal has several fairly customizable options for giving users an opportunity to send money on a web site.  Even with their simple “button” feature, I was surprised at how much customization you could do.  You can easily create buttons to “Buy Now,” “Subscribe,” “Donate,” and even “Add to Cart.”  Some of the buttons even allow you to specify purchasing options for an item.  For example, you could have a button with “Small,” “Medium,” and “Large,” with each option having a separate checkout price.

To try it out, I decided to make a custom button for my website.  I chose the “Donation” button, which allows the buyer to specify the amount of money they wish to send to me.  I didn’t really want to use the word “Donate,” so I copied their default Donate button into Paint.NET and edited out “Donate” and added “Pay Me!”

I also added a custom header to the checkout page so it (somewhat) matches my blog theme.  However, because the image is hosted in a non-https domain, users may see an error that some of the content being loaded did not come from a secure source.  I’ll live.

Collecting money via PayPal is super easy.  If you want to see the fruits of my labors, check out the “Pay Me!” section of my website.

image

Categories: General Tags:

An Everything-Glazed Lunch: A HoneyBaked Krispy Kreme Sandwich

March 28th, 2010 Ben McCormack 5 comments

HoneyBaked Krispy Kreme Sandwich 

Since my new job began a month ago, I’ve been trying to experiment with the World’s Best Ham and try it in different food combinations.  After picking up a box of Krispy Kreme doughnuts last night, I was intrigued by the fact that a Krispy Kreme doughnut is basically bread covered in a sugar glaze:

The World's Best Doughnut

HoneyBaked Ham is basically a ham (well, not just a ham; this is the World’s Best Ham that we’re talking about here) covered in a sugar glaze (can’t you just taste the crunch? yum…)

The World's Best Ham

A sandwich, to be overly generalistic, is simply two pieces of bread with a piece of meat in between.  So what do you get when you take two glazed pieces of bread and put a glazed piece of meat in between? I present to you the HoneyBaked Krispy Kreme Sandwich:

The sandwich, squished and ready to eat

To be honest, I thought that combining the World’s Best Ham with the World’s Best Doughnut (IMHO) could possibly make the World’s Best Sandwich.  It turns out that there’s just a little too much sugar in this culinary concoction as it hides the natural tastiness of the ham.  Perhaps this would make a nice treat at state fairs and southern weddings.

While my final verdict for the sugary sandwich is only a C+, I still enjoyed the experiment and it made for a nice high-calorie Sunday brunch.

Yummm!

Categories: General Tags:

Finding a Job the Unconventional Way

February 22nd, 2010 Ben McCormack 6 comments

image

Sometime at the end of last summer, I was sharing with my newlywed wife that I wanted to transition into a career as a software developer.  With no professional background in programming and only being able to put on a resume what I’ve taught myself in Excel and Access VBA code, a little HTML, and some SQL, deciding to find a job as a developer was no small endeavor. 

With the encouragement of a friend, I decided to start learning Silverlight and even blogged about my learning (the post originated at Silverenlightening.com and has been ported here).  I read tons of blog posts that focused on Silverlight and software development in general.  I tried reading a book on Silverlight and when I realized that Silverlight was over my head because of my lack of programming experience, I bought a book on C# and got some great practice over at ProjectEuler.net.  I went to several different users groups each month that focused on Microsoft technologies.   I started using new technologies at work to leverage the power and flexibility of the more advanced programming frameworks.  I asked lots of questions and even provided a few answers over at StackOverflow.com, an online community where programmers can ask questions to other programmers.  And I started following lots of software developers on Twitter.

I wasn’t planning on searching for a new job until the summer of 2010.  I figured that a year of practice and learning would put me in a good position to enter the market for a job as a junior software developer.  That was until I saw this tweet from the CEO of Fog Creek Software announcing a job for a support engineer:

image

“Fog Creek? I can’t work there,” I initially thought, “they only hire the best and brightest developers on the planet!” As I read through the job description, I realized that I didn’t have to be a professional software developer to support professional software.  I just had to be smart and able to solve problems. Great!

I completed my first resume that evening, which led to a phone interview with the head of technical support at Fog Creek.  That interview went really well and eventually I was invited to New York for an in-person interview.  Although I wasn’t offered a job, the whole interview process was wonderful and I left New York with tons of confidence that I could find a new job, even with my limited background in the field of software development.

I continued to do the same things I was doing before going to New York: reading blogs, practicing programming, going to users groups, and keeping my eye on Twitter.  While I usually don’t pay much attention to when people I don’t know follow me on Twitter, when Kelly Thielemann started following me on Twitter, I decided to follow her as well.  I didn’t know much about her other than the fact that she was a recruiter for Matrix, a company I had heard about after going to the .NET users group, she tweeted about new jobs every now and then, and I’m pretty sure she went to my high school (though not a requirement when following someone on twitter, I’m more likely to follow someone local than someone halfway around the planet if they’re following me for marketing or sales reasons).

When I think back on it, I’m not sure what inspired me to click on the following tweet:

image

Sure, Java is similar to C#, but I’m by no means a “Java dev.” Maybe it was the “Jr” part of the job that made me click through (if I’m anything in the world of software development, I’m certainly junior).  The job description seemed like it might be similar to the Fog Creek job, so I figured it wouldn’t hurt to send in my resume.

The senior recruiter at Matrix who reviewed my resume saw very quickly that I wasn’t a good fit for the Java job (you actually had to know Java; who knew?).  However, she suggested another opportunity for a junior development position at HoneyBaked Ham Company.  It sounded like a great job that would provide a way to begin a career as a software developer, so I decided to pursue it.

The phone interview went really well and that led very quickly to an in-person interview.  After meeting with their lead developer and VP of IT, I felt very strongly that this job would be a great fit for me and was really hoping for an offer.  While waiting a few days to hear back from Matrix after the interview, I felt like I was dating again; I thought the anxious feeling wondering if the other person was going to call wouldn’t be an issue anymore when I got married.  Thankfully, I heard back from Matrix and received an offer!

After discussing the offer with my wife, I accepted the job and will start at HoneyBaked on March 1st.  I’m amazed at how things lined up so that I was able to get connected with HoneyBaked.  I followed blogs, networked at users groups, and kept up with industry trends on Twitter.  I didn’t go to any job fairs, use jobs-centered websites, or really do any of the conventional things that people do to look for jobs.  I guess that in today’s market, it doesn’t hurt to be a little unconventional.

Categories: General Tags:

Using Twitter to Find a Job

February 18th, 2010 Ben McCormack 2 comments

I’ve been meaning to write up a post about my success in finding a job to begin my career in software development. Matrix, the staffing/recruiting company I worked with to find the job, wrote up a post about how I used Twitter to find a job. Check it out!

Using Twitter to Find a Job – A Real Life Example

Categories: General Tags:

A Valentine’s Day Letter To My Wife

February 14th, 2010 Ben McCormack 2 comments

IMG_8900

I’ve never been a huge fan of Valentine’s Day, so when I was reading the opinion section of the Atlanta Journal Constitution this past Thursday and came across an article titled “Love, yes, but be committed to marriage,” it really resonated with me.  The Honorable Leah Ward Sears, who wrote the article, challenges readers on the very first line:

Here’s a word of advice this Valentine’s Day: Don’t marry for love.

She goes on to write about how commitment, not necessarily love, is the most important part of keeping marriage alive.  In fact, she even goes so far as to suggest that committing to the health of the marriage is more important than committing to each other.  Those are some bold words, and after thinking about it and testing her wisdom in the context of my own young marriage, I can’t help but agree with her.

In reflecting on what to give my wife for our first Valentine’s Day as a married couple, I chose to write her a letter.  Influenced by the article I had read, the letter shares a lot of my thoughts about marriage, love, and even a few nuggets about Valentine’s day.  For those who have the stomach to digest a man’s love-letter to his newly-wed wife, here’s what I wrote to her (shared, of course, with her permission):

Read more…

Categories: General Tags:

My First Attempt At Theming WordPress: FreesiaFloralDesign.com

February 2nd, 2010 Ben McCormack No comments

A week ago today, I got a phone call from my mother-in-law asking if I might be able to help her get her website working within a week.  Things didn’t work out with her original website and it was going to be taken down within a week.  Valentine’s day is fast approaching, and she needs a functional website. Now.

I’ve dabbled with the CSS on my own WordPress blog (though I never uploaded it to the server), but theming a website from scratch was a whole new animal. Could I do it?

I decided to say yes.

Building on WordPress has myriad advantages, not the least of which is empowering the client to be able to mange most of the content herself.  Sure, you don’t want your client to do everything, but it’s nice when the developer/designer doesn’t have to log in to change spelling, add a paragraph, etc.  I get to do the big things.  She can do all the little details.

So where did I start?

Read more…

Categories: General Tags:

Worth It: Microsoft 4000 Keyboard

January 22nd, 2010 Ben McCormack No comments

Several months ago, I was on twitter and noticed a few people singing the praises of the Microsoft 4000 Natural Ergonomic Keyboard :

 

image

I was never particularly fond of ergonomic keyboards, mainly because they feel awkward at first when compared to traditional keyboards, and also because they are not well designed for computer gaming (which is perfectly fine…I believe they’re designed for typing; my priorities were much different when I was younger).

After following the conversation on twitter, someone suggested that if using a computer (and specifically typing on a keyboard) makes up a large part of your work day, you owe it to yourself to have a good mouse and keyboard.  This makes perfect sense, but I had never given much thought to the keyboards I used other than the basic fact that I needed it to use my computer.

As I’ve been trying to do more work that involves writing code, I noticed my wrists and hands starting to get sore at the end of the work day.  Since I use my computer as an essential part of my job, I decided an ergonomic keyboard might help in the long term to reduce injury to my hands and possibly even improve my typing.  When Dell had a sale on the keyboards, I decided to jump on it and buy one for work and one for the office.

While it was certainly awkward at first to position my hands angled slightly inward and tilted slightly forward at the wrist, I immediately noticed that this keyboard provides a much more natural physical position for typing than do traditional keyboards.  Using this keyboard also quickly highlighted some bad typing habits that I had acquired over years of computer use.  When you can’t use your left hand to press ‘Y’ because of a large gap in the middle of the keyboard, you quickly adjust your typing habits.

I’m very glad to say that after a month of everyday use, I’ve really come to appreciate this keyboard.  I’m certain that my typing is much faster and that I’m using better technique.  I also sometimes use the macro functionality built-in to the keyboard (though must of the other “extras” are left unused).  Overall, this keyboard has been a fantastic investment.

If you’re in a field that requires a lot of typing, you owe it to yourself to give this keyboard a try.

Notes and Reflections From the Atlanta WordCamp

January 10th, 2010 Ben McCormack 1 comment

Last Wednesday, I noticed the following tweet from local Microsoft Developer Evangelist, Glen Gordon:

glengordon_tweet

After I installed WordPress on my local Windows 7 machine (which went fine, except for a weird bug with Skype), I received a complimentary pass to attend the WordCamp.

While I tend to lean towards Microsoft technologies, my blog is in fact running WordPress, so I was glad to have the opportunity to attend a conference to bring me up to speed on the publishing platform.  My hope was to learn about building themes, writing plug-ins, and maybe even pick up a little PHP.

The first thing I noticed about the conference was how different the demographic was from the Microsoft users groups I had attended.  The average age of the attendees was quite young and there were lots of women.  The feeling of community was palpable throughout the event.

Overall, I was glad I went, even though the actual sessions seemed somewhat weak in demonstrating how to actually do stuff in WordPress.  The presenters of the sessions I attended were good, but they were heavy with PowerPoint slides and I would have appreciated some actual walk-throughs, and maybe even a peek at some actual code.  Perhaps that’s the developer bias within me that likes to see things demonstrated.

 

People

 

I met some wonderful people throughout the day and this was by far this biggest reward on my time. 

  • Chef Darin Sehnert – I know Darin from a cooking class that he taught while my wife and I were on our honeymoon.  His cooking classes are worth a trip to Savannah.
  • Pam Leinmiller – I had lunch with Pam and learned about her background in computer science and her plans to start a blog about positive health living.
  • Glen Gordon – I enjoyed meeting Glenn and discussing Microsoft at a Word Press conference :-).  I really appreciated his sharing about the Microsoft WebsiteSpark program.
  • Moses Ngone –  I met Moses at the “Genius Bar” and he was incredibly helpful in teaching me about a lot of the “developer” stuff that goes into WordPress.  He actually sat down with me and showed me some code, helping to build my confidence about diving into HTML, CSS, and PHP.
  • Nathan Ketsdever – After I arrived at TAP for the after party (thanks sponsors!), I noticed that Nathan was ordering the same beer I was intending to order, the Left Hand Milk Stout (though I think we actually ended up drinking the Red Brick Ale 15th Anniversary Edition. Waiter, thanks for the mistake; it was amazing).
  • Mitch Canter – I talked to Mitch briefly as he was headed out the door.  His session was my favorite because he got out of PowerPoint and talked about how he develops themes.  I really appreciated hearing about his approach to web design.
  • Matt Thomas – I met Matt towards the end of the evening.  He’s the designer behind WordPress.com.  I especially enjoyed geeking out with him about typography and fonts!

    Notes

     

     

    Final Thoughts

     

    I had a really wonderful time at the WordCamp and can’t wait to get started hacking away at my blog.  Thanks to everyone for all of their hard word and inspiration!

    Categories: General Tags:

    Using Project Euler to Learn a Programming Language

    January 5th, 2010 Ben McCormack No comments

    When I first began learning to develop Silverlight applications, I noticed very quickly that I struggled a lot with C#, even those things that most programmers would consider very basic, such as:

    • i += j is the same as i = i + j;
    • Angle Brackets < > usually identify a generic where a type ought to be specified.
    • What in the world is LINQ?
    • What does static mean?

    Since I was struggling so much with C#, I decided to read Illustrated C# 2008 by Daniel Solis.  While it was a great introduction to the language, I still found myself hungering for a way to use C# and thus test my newfound knowledge.  I started a few small projects, but I wasn’t getting excited about working on anything.  I’m talking about the kind of excitement where you don’t want to leave work or go to bed at night because you’re having so much fun writing code.

    Then I found Project Euler.

    Read more…

    Categories: General Tags: