Home

Advertisement

I may just be getting a life!

  • 16th Apr, 2007 at 8:31 AM
pic#Me
So I've been pretty much holed up behind my desk at work for the past month or so. As a result I haven't had the time to shave my new budding beard let alone post new articles of fantastic insight. However, the project I have been working on is as close to being finished as to make no difference. So, in light of this, not only shall I be posting more often but I may even be out and about on weekends! This is a novel experience.

Tags:

pic#Me
This initially confused me for a few days. I had a bunch of cookies created via my AJAX handler and I simply couldn't find the little buggers.
Neither could I find the session or cache. This was quite a serious problem for me since well, nothing works if you can't find the values you need. Then after much searching I found out about this little gem...

System.Web.SessionState;

This is a remarkable useful library since it allows you to add the following to your ashx handler

using System;
using System.Web;
using System.Web.SessionState;

public class Handler : IHttpHandler , IReadOnlySessionState
{
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        context.Response.Write("Hello World");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}


Without IReadOnlySessionState or IRequiresSessionState you cannot control how you access the session through your handler.
For instance, IRequireSessionState grants you access to the Response and Request objects in the current context which in turn, makes life that much easier.

Tags:

Now, what's in the the code-behind?

  • 15th Mar, 2007 at 8:33 AM
pic#Me
Ok, so now we know a little about creating a client script to utilize the XMLHttpObject.
Fantastic... but what about the server-side nonsense? Well...

You will need to create a Web Handler ( .ashx )
This kind of file allows you to delve into the code without mucking about with HTMLnonsense and browser problems since its output is through an XML consuming client of sorts.

Therefore, this kind of file is great. Here's an example of one.
NB: You need to include the IsReusable function for obvious reasons

<% @ webhandler language="C#" class="AverageHandler" %>

using System;
using System.Web;

public class AJAXHandler : IHttpHandler
{
    public bool IsReusable
    {
        get
        {
            return true;
        }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.Write("hello");
    }
}

So the ProcessRequest function does any processing you need done and then returns the value of it.
This is all well and good but the last piece of the puzzle is the actual JavaScript call to get your JavaScript library interacting with your
new handler.

That call is as follows:

AjaxGetData('../Handlers/AjaxHandler.ashx', helloWorldHandler);

If you throw this into any event on any client-side control, it will kick off your handler and get it all done in the blink of an eye with the XMLHttpRequest object. Oh, and please don't forget that on the actual page on which you have your control, you also need an elelment with an ID of whatever you specify in your JavaScript libraries handler (i.e.: helloWorldHandler displays its output in an element call 'div-hello'

Tags:

So how do I use this AJAX thing anyway?

  • 15th Mar, 2007 at 7:55 AM
pic#Me
So, right off the bat, you need to know or at least understand JavaScript in order to make use of AJAX.
It all works off a little known object called the XMLHttpRequest. This beauty allows you to send a request and receive a response as opposed to the usual sending of a request and getting a whole page back. Much cheering all round...

Anyhow, I recommend creating a JavaScript library for this so that you can use it over and over again without much typing. Yay!

So, without any further delay, here's the code.

function AjaxGetData(url, responseHandler)
{
    if (window.XMLHttpRequest)
     {
         // browser has native support for XMLHttpRequest object
         req = new XMLHttpRequest();
     }
     else if (window.ActiveXObject)
     {
         // try XMLHTTP ActiveX (Internet Explorer) version
         req = new ActiveXObject("Microsoft.XMLHTTP");
     }
    
     if(req)
     {
         req.onreadystatechange = responseHandler;
         req.open('GET', url, true);
         req.setRequestHeader("content-type","application/x-www-form-urlencoded");
         req.send('');
     }
     else
     {
         alert('Your browser does not seem to support XMLHttpRequest.');
     }
 }

function helloWorldHandler()
 {
 try
    {
        //readyState of 4 or 'complete' represents
        //that data has been returned

        if (req.readyState == 4 ||
            req.readyState == 'complete')
        {
           
document.getElementById('div-hello').innerHTML = req.responseText;
        }
    }
    catch(e)
    {
        alert('Error in Ajax response');
    }
}


First off, you create an XMLHttpRequest object, pass it a url, and a function to handle the return data ( i.e.: helloWorldHandler )

The return handler simply checks if the request was successful and if so, prints the return value to the screen
( document.getElementById('div-hello').innerHTML = req.responseText; )

Very useful if you ask me. Anyhow, I suppose I should do some work about now...
More to come on this topic. Perhaps a bit on the server-side of this kind of thing

Tags:

So I've decided that AJAX rocks my world

  • 14th Mar, 2007 at 4:14 PM
pic#Me
So I've been using the Microsoft AJAX library for some time now and every release they've made made my life that much easier.
Then I started a new project for a MAJOR client and it seems the AJAX toolkit, (extensions included) just wasn't enough.

After much head-scratching I finally delved into the depths of the XMLHttpRequest object. Initially it allowed me the destroy everything I was supposed to fix. Woe...

Then I started learning a bit more about how it actually works. Can you imagine anything greater than being able to process anything on the server-side whilst being called from the client-side? Oh and it returns to the client-side too. Now by client-side I mean anything that does not have that oft forgotten tag runat="server".

Fantastic!

Anyhow, when I have a little more time I shall be posting the intricacies of using your own XMLHttpObject in all its splendor.

Tags:

Right, so I've finally bothered

  • 13th Mar, 2007 at 5:16 PM
pic#Me
Alright, I've finally gotten onto LJ after much much time refusing to do so. However, the endless hours at work are beginning to wear me down and I guess I probably do need something to do to calm me down at times.

After the next few weeks I suppose I should start doing some changes and making everything look pretty. From there, maybe I'll post something interesting.

Latest Month

April 2007
S M T W T F S
1234567
891011121314
15161718192021
22232425262728
2930