Skip to Main Content

Monday, April 17, 2006

Quick AJAX Overview

For those precious few who have held out on learning about AJAX, here is a quick overview of how to implement it.

AJAX Example


One of the first things you will need to do is to reference a function on an HTML element's event. In our example, we will use the <body> tag's onload event:

  1. <body onload="getData();">

Next, we create the getData() function to establish the AJAX connection and retrieve the data from the server (in this case, its an XML document).


  1. var ajax;
  2.  
  3. function getData()
  4. {
  5. //Checks if it's a modern browser
  6. if(document.getElementById)
  7. {
  8. //Creates the AJAX Object bases on browser type
  9. if(window.ActiveXControl)
  10. {
  11. ajax = new ActiveXOject("Microsoft.XMLHTTP");
  12. }
  13. else
  14. {
  15. ajax = new XMLHttpRequest();
  16. }
  17.  
  18. //If the ajax object was created successfully
  19. if(ajax)
  20. {
  21. //Process the response (calls the processResponse function)
  22. ajax.onreadystatechange = processResponse;
  23.  
  24. //XML/RSS/Web service URL
  25. ajax.open("GET", "/services/data.xml", true);
  26.  
  27. //Parameters to send to the URL (null in this case)
  28. ajax.send(null);
  29. }
  30. }
  31. }

If you notice, I'm using a .xml file. This could also be a web service, an rss feed, or even just a web page that returns xml.

Next, we create the "processResponse" function. In the code example above, we assign the ajax.onreadystatechange a reference to a function. This function will provide a way to access the response of the xml returned.


  1. function processResponse()
  2. {
  3. if(ajax.readyState == 4 && ajax.status == 200)
  4. {
  5. //Assign the XML returned to a local variable
  6. var xml = ajax.responseXML;
  7. alert(xml);
  8. }
  9. }


While the above example doesn't do anything fancy, it demonstrates how easy it is to create a quick asynchronous connection to get some data without refreshing the page.

I will be creating an XMLHttpRequest tutorial in the next post, so look forward to learning more AJAX.

Adios

Thursday, April 13, 2006

Change Meta Tags Programmatically in ASP.NET 2.0

There is a problem in ASP.NET 2.0. The problem is that when you use MasterPages in an .aspx file, you cannot override the <meta> tags.

The issue that I came across was that I had a <meta name="description"> tag in my MasterPage file for use by search engines. It's nice to display a paragraph saying exactly what you want people to know about a particular page/site. However, if you want to change the description on every page, you are out of luck. Visual Studio 2005 tricked me into thinking it was nice and easy. How?

Well, if you want to change the title of each page that uses a MasterPage, you can specify the title in the @Page directive as follows:

  1. <%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master"
    CodeFile="default.aspx.cs" Inherits="_default" Title="My Page Title" %>

However, I was tricked into think I could change the meta description because there is a Description attribute in the @Page directive just like title.

To get around this, you must make the change programmatically. There are two ways to do it:
  1. Create a HtmlMeta control for each page, set the properties, and add the control the the page's Header object.

  2. Create a public method in the MasterPage that changes the meta tag and call that method from each page.

Personally, I chose the latter (Number 2) because it was just a little bit less code.

Here's the code:
In the MasterPage.aspx page, I created a <meta> tag in the <head> section:
  1. <meta runat="server" name="description" id="description">

Next, in the MasterPage.aspx.cs file, I created a public method that takes a string parameter and replaces the description with the parameter:
  1. public void ChangeMetaDescription(string description)
  2. {
  3. description.Attributes["content"] = description;
  4. }

Finally, in my .aspx.cs file, I called the ChangeMetaDescription() method in the Page_Load event:
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. MasterPage master = Master as MasterPage;
  4. master.ChangeMetaDescription("This is my new description.");
  5. }

In Conclusion
Although I used this strictly for the meta-description, you can use this method for any control inside the MasterPage.

Friday, March 10, 2006

Move Over .NET and Java, Here's PHP

Zend Technologies has just released version 0.1.1 of its new PHP framework called the PHP Collaboration Project. This framework was designed to rival the .NET and Java frameworks and rise PHP to the same level as those same languages.

The PHP framework takes advantage of many of the new PHP 5 features, including XML, RSS, and WebService API's.

KPL - Kids Programming Langauge

Kid's Programming Language, KPL, is a programming language designed to make programming fun and exciting for kids. In fact, it was designed to make computer games. Well, not soley computer games, but it makes it easy to make games. If you think about it, kids would really enjoy programming a lot more if they were designing a game instead of a email client or something.

At first glance, it looks very simliar to Visual Basic, and for good reason. It was modeled after VB.NET. The IDE was also modeled after Visual Studio.NET. Now, for you .NET haters out there, please don't let that discourage you. Remember, this is a programming language for kids with the main objective of teaching young kids the basic concepts of a computer language.

Microsoft has started officially supporting and promoting KPL since September of 2005 and has since integrated it into it's Coding4Fun site on MSDN. Their site includes games, articles, and challeges.

For an example of a "Hello World!" application, you just need 5 lines of code:

Program HelloWorld
Method Main()
Print("Hello World!")
End Method
End Program

There is so much more you can do with KPL, but you can visit their site for a complete list of functions.

Wednesday, March 08, 2006

Why I Hate Frameworks

I came across this posting on the net and thought it was amazingly brilliant. So, for your entertainment pleasure, "Why I Hate Frameworks", by Joel Spolsky:

I'm currently in the planning stages of building a hosted Java web application (yes, it has to be Java, for a variety of reasons that I don't feel like going into right now). In the process, I'm evaluating a bunch of J2EE portlet-enabled JSR-compliant MVC role-based CMS web service application container frameworks.

And after spending dozens of hours reading through feature lists and documentation, I'm ready to gouge out my eyes.

Let's pretend I've decided to build a spice rack.

I've done small woodworking projects before, and I think I have a pretty good idea of what I need: some wood and a few basic tools: a tape measure, a saw, a level, and a hammer.

If I were going to build a whole house, rather than just a spice rack, I'd still need a tape measure, a saw, a level, and a hammer (among other things).

So I go to the hardware store to buy the tools, and I ask the sales clerk where I can find a hammer.

"A hammer?" he asks. "Nobody really buys hammers anymore. They're kind of old fashioned."

Surprised at this development, I ask him why.

"Well, the problem with hammers is that there are so many different kinds. Sledge hammers, claw hammers, ball-peen hammers. What if you bought one kind of hammer and then realized that you needed a different kind of hammer later? You'd have to buy a separate hammer for your next task. As it turns out, most people really want a single hammer that can handle all of the different kinds of hammering tasks you might encounter in your life."

"Hmmmmmm. Well, I suppose that sounds all right. Can you show me where to find a Universal Hammer."

"No, we don't sell those anymore. They're pretty obsolete."

"Really? I thought you just said that the Universal Hammer was the wave of the future."

"As it turns out, if you make only one kind of hammer, capable of performing all the same tasks as all those different kinds of hammers, then it isn't very good at any of them. Driving a nail with a sledgehammer isn't very effective. And, if you want to kill your ex-girlfriend, there's really no substitute for a ball-peen hammer."

"That's true. So, if nobody buys Universal Hammers anymore, and if you're no longer selling all those old-fashioned kinds of hammers, what kinds of hammers do you sell?"

"Actually, we don't sell hammers at all."

"So..."

"According to our research, what people really needed wasn't a Universal Hammer after all. It's always better to have the right kind of hammer for the job. So, we started selling hammer factories, capable of producing whatever kind of hammers you might be interested in using. All you need to do is staff the hammer factory with workers, activate the machinery, buy the raw materials, pay the utility bills, and PRESTO...you'll have *exactly* the kind of hammer you need in no time flat."

"But I don't really want to buy a hammer factory..."

"That's good. Because we don't sell them anymore."

"But I thought you just said..."

"We discovered that most people don't actually need an entire hammer factory. Some people, for example, will never need a ball-peen hammer. (Maybe they've never had ex-girlfriends. Or maybe they killed them with icepicks instead.) So there's no point in someone buying a hammer factory that can produce every kind of hammer under the sun."

"Yeah, that makes a lot of sense."

"So, instead, we started selling schematic diagrams for hammer factories, enabling our clients to build their own hammer factories, custom engineered to manufacture only the kinds of hammers that they would actually need."

"Let me guess. You don't sell those anymore."

"Nope. Sure don't. As it turns out, people don't want to build an entire factory just to manufacture a couple of hammers. Leave the factory-building up to the factory-building experts, that's what I always say!!"

"And I would agree with you there."

"Yup. So we stopped selling those schematics and started selling hammer-factory-building factories. Each hammer factory factory is built for you by the top experts in the hammer factory factory business, so you don't need to worry about all the details that go into building a factory. Yet you still get all the benefits of having your own customized hammer factory, churning out your own customized hammers, according to your own specific hammer designs."

"Well, that doesn't really..."

"I know what you're going to say!! ...and we don't sell those anymore either. For some reason, not many people were buying the hammer factory factories, so we came up with a new solution to address the problem."

"Uh huh."

"When we stepped back and looked at the global tool infrastructure, we determined that people were frustrated with having to manage and operate a hammer factory factory, as well as the hammer factory that it produced. That kind of overhead can get pretty cumbersome when you deal with the likely scenario of also operating a tape measure factory factory, a saw factory factory, and a level factory factory, not to mention a lumber manufacturing conglomerate holding company. When we really looked at the situation, we determined that that's just too complex for someone who really just wants to build a spice rack."

"Yeah, no kidding."

"So this week, we're introducing a general-purpose tool-building factory factory factory, so that all of your different tool factory factories can be produced by a single, unified factory. The factory factory factory will produce only the tool factory factories that you actually need, and each of those factory factories will produce a single factory based on your custom tool specifications. The final set of tools that emerge from this process will be the ideal tools for your particular project. You'll have *exactly* the hammer you need, and exactly the right tape measure for your task, all at the press of a button (though you may also have to deploy a few *configuration files* to make it all work according to your expectations)."

"So you don't have any hammers? None at all?"

"No. If you really want a high-quality, industrially engineered spice rack, you desperately need something more advanced than a simple hammer from a rinky-dink hardware store."

"And this is the way everyone is doing it now? Everyone is using a general-purpose tool-building factory factory factory now, whenever they need a hammer?"

"Yes."

"Well�All right. I guess that's what I'll have to do. If this is the way things are done now, I guess I'd better learn how to do it."

"Good for you!!"

"This thing comes with documentation, right?"