views:

522

answers:

5

Hi,

I am just wondering that, is there anyway to make an Ajax request using jquery ("$.ajax") and making partial rendering without using the .NET Ajax framework (without script manager).

I have tried this before, but it's executing the page_load every time and not reaching to the pagemethod.

function doAsync() 
{ 
   jQuery.ajax({ 
   type: "POST", 
   url: "/WebForm1.aspx/testMethod", 
   error: function (xhr, status, error) { alert(status); }, 
   success: function (response) { alert('suc'); }
   )};
}

 [WebMethod] 
 public static void testMethod() 
 {
     //server side code 
 } 

Is there anything wrong here?

Thanks!

A: 

Well how webforms is setup you can't actually get to the code behind file since that would require you to go through the entire page life cycle. You can't just have some method in your code behind file and try to just target it.

So you need to either make a web service or what I like to do is use a generic handler.

So you would go to add - add new item - generic handler(.ashx)

It would generate a file like this(the follow code has some more stuff in it that is not generated by default).

<%@ WebHandler Language="C#" Class="FileNameOfGenricHandler" %>

using System;
using System.Web;

public class FileNameOfGenricHandler: IHttpHandler {

    public void ProcessRequest (HttpContext context) {

        // use context.Request to get your parameter fields that you send through ajax.
        int ID =Convert.ToInt32(context.Request["sendID"]);

        // do something with the variable you sent.

       // return it as a string - respone is just a string of anything could be "hi"
       context.Response.ContentType = "text/plain";
       context.Response.Write(response);

    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

On jquery side

  // Post to the path like this ../FolderName/FolderName/FileName.ashx(or whatever you stuck your generic handler file).
    $.post('../FolderName/FileNameOfGenricHandler.ashx', { sendID: id }, function(response)
                {
                     alert(response)
});

One downside with the generic handler though is for every different ajax request you want to make you will need its own generic handler. So if you want to create a user, delete a user and update a user through ajax you will have to have 3 generic handlers.

Here is a similar tutorial I used.

http://sites.google.com/site/spyderhoodcommunity/tech-stuff/usingjqueryinaspnetappswithhttphandlersashx

chobo2
This is correct, we can't just reach a method, it will go through the whole page cycle.
Thurein
Hence why I am using a generic handler. It is different the an .aspx page it does not have a life cycle. In the case I am using it as. It is one method in the file. That way you can do exactly what you need. If you don't like the idea of making many generic files. Make a web service application in your project. Make all your methods in this web service application and do all your ajax requests to this webservice and its methods.
chobo2
That's not correct. Though its code resides in the code-behind file for a given ASPX page, the page life cycle isn't executed when you call an ASP.NET AJAX "page method". For all intents and purposes, page methods are shorthand for ASMX ScriptServices.
Dave Ward
You can use [WebMethod] attribute to reach any method on the Aspx page. Or you can use different ways and Dave Ward at Encosia talks about almost all the cool ways to do so.
Braveyard
First I don't know why I got marked down. I don't know if it is because I said you can't stop the page life cycle or if you guys don't like my way of doing it. The ASMX ScriptService seems to always need the ScriptManager(correct me if I am wrong) what "Thurein" asked not to use. That makes total sense since if I am using jquery I personally don't want to have the ScriptManager in my code either since I probably won't be using the asp.net ajax stuff so I think it is more baggage.
chobo2
A ScriptManager isn't required for ASMX ScriptServices or for page methods. See http://encosia.com/69 and http://encosia.com/77 for examples.
Dave Ward
+3  A: 

Consuming Web Services through jQuery Ajax methods :

http://tinyurl.com/34jty7

Consuming Methods residing on an Aspx Page through jQuery Ajax Methods

http://tinyurl.com/6z98l6

Actually the articles above are the ones that taught me jQuery Ajax truly. If it is your first time, don't worry ! They are really easy to comprehend.

jQuery Ajax Methods :

  • ajax() : The low-level ajax method for every ajax call
  • post() : Specialized for post methods
  • get() : Specialized for get methods
  • getJSON(): Specialized for get json type result set
  • load() : To Inject html portion into html element on the page.
Braveyard
A: 

You can do this using page methods. Basically, you create a static method and add the WebMethod attribute and use it as if it were a web service method.

Gabriel McAdams
+2  A: 
rahul
+13  A: 

What you're missing is the content-type. To make a request to a page method, you must make the call exactly like this:

$.ajax({
  type: "POST",
  url: "/WebForm1.aspx/testMethod",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting here.
  }
});

See this for more detailed information: http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Dave Ward
I have added the content-type but its still executing the page_load method and not reaching to the web method in my aspx code behind.
Thurein
Be sure that you have your web.config properly configured for ASP.NET AJAX. You definitely can call page methods in this manner, without running the page through its life cycle.
Dave Ward
Now i think, we got the point, because my site is not ajax enabled, and that was also the reason I was trying to use the jquery ajax call, any suggestion. Thanks
Thurein
What version of .NET are you running?
Dave Ward
Yeah, I think it's your web.config that's missing the stuff it needs to do ASP.NET Ajax. Try this: switch your form to Design View, and drag a Script Manager control onto it. Then switch back to Source View and remove the Script Manager. VS will update your config file when you add that control to your form.
MrDustpan
I am using .net 2.0 and its not ajax enabled.
Thurein
Dave Ward