Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET JavaScript Callbacks Without Full PostBacks?

I'm about to start a fairly Ajax heavy feature in my company's application. What I need to do is make an Ajax callback every few minutes a user has been on the page.

  • I don't need to do any DOM updates before, after, or during the callbacks.
  • I don't need any information from the page, just from a site cookie which should always be sent with requests anyway, and an ID value.

What I'm curious to find out, is if there is any clean and simple way to make a JavaScript Ajax callback to an ASP.NET page without posting back the rest of the information on the page. I'd like to not have to do this if it is possible.

I really just want to be able to call a single method on the page, nothing else.

Also, I'm restricted to ASP.NET 2.0 so I can't use any of the new 3.5 framework ASP AJAX features, although I can use the ASP AJAX extensions for the 2.0 framework.

UPDATE
I've decided to accept DanP's answer as it seems to be exactly what I'm looking for. Our site already uses jQuery for some things so I'll probably use jQuery for making requests since in my experience it seems to perform much better than ASP's AJAX framework does.

What do you think would be the best method of transferring data to the IHttpHandler? Should I add variables to the query string or POST the data I need to send?

The only thing I think I have to send is a single ID, but I can't decide what the best method is to send the ID and have the IHttpHandler handle it. I'd like to come up with a solution that would prevent a person with basic computer skills from accidentally or intentionally accessing the page directly or repeating requests. Is this possible?

like image 234
Dan Herbert Avatar asked Aug 26 '08 02:08

Dan Herbert


2 Answers

If you don't want to create a blank page, you could call a IHttpHandler (ashx) file:

public class RSSHandler : IHttpHandler
    {
        public void ProcessRequest (HttpContext context)
        {   
            context.Response.ContentType = "text/xml";

            string sXml = BuildXMLString(); //not showing this function, 
            //but it creates the XML string
            context.Response.Write( sXml );
        }

        public bool IsReusable
        {
            get { return true; }
        }

    }
like image 97
Daniel Pollard Avatar answered Sep 22 '22 12:09

Daniel Pollard


You should use ASP.Net Callbacks which were introduced in Asp.Net 2.0. Here is an article that should get you set to go:

Implementing Client Callbacks Programmatically Without Postbacks in ASP.NET Web Pages

Edit: Also look at this: ICallback & JSON Based JavaScript Serialization

like image 26
Ricky Avatar answered Sep 21 '22 12:09

Ricky