1

I am using Jquery for posting form data in aspx.

the way i have adopted is, I make one page having form and another aspx page where i receive values and in its page_load i run the query or other logic.

I am using $.ajax method of jquery, but if there some exception or issue occurs, it does not respond correctly and the preloader kept on working.

Please tell me if there is any supportive way in .Net, so i can use this jquery $.ajax in a proper way, or should i use webservices or whatever please let me know, or can I call code behind c# function from jquery $.ajax? Thanks Atif

1
  • I've read your question and your comments on people's answers and I'm not sure what you're asking anymore. You said in one comment "but if some exception occurs then this does not work, i also want if the receiving code could give some intellisense like it gives in code behind file." - What do you mean intellisense? Do you want intellisense in your javascript files for the jQuery library? Commented Jul 2, 2011 at 4:08

4 Answers 4

1
   //the data you want to post
    var dataObject = JSON.stringify(reqObject); /// to use this you need json plugin link is posted below



    //Call the page method  
    $.ajax({
        async: false,
        type: "POST",
        url: youraspxPage + "/" + your code behind function name,
        contentType: "application/json;",
        data: dataObject,
        dataType: "json",
        success: ajaxCallSuccess,
        error: ajaxCallFailure
    });


function ajaxCallSuccess(response) {
    var msg1 = response.d;
}

function ajaxCallFailure(response) {
    var msg2 = response.d;
}

try this let me know if it works...

json plugin

see this

Sign up to request clarification or add additional context in comments.

2 Comments

I have written like this, its not working, please check $.ajax({ async: false, type: "POST", url: "Schedular.aspx/check", contentType: "application/json;", data: dataObject, dataType: "json", success: ajaxCallSuccess, error: ajaxCallFailure });
method you are posting to is it a web method?
0

The extra page is unnecessary. If you'd like to really do ajax, you can post to a ScriptMethod(http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute.aspx) in the same class. It means you don't need to wire up all that other stuff involved in having an Aspx. This method is a good start into doing ajax in Asp.Net.

1 Comment

The ajax of asp.net is very much slow as compared to jquery $.ajax, and asp.net ajax does not refresh page but runs whole c# file behind.
0

the .apsx page with ajax need to call a handler (.asxh) to get the values and process it

you can put the handler in one location or in the same location as the aspx page that is calling it

the ajax would retrieve the values and send it to the hanlder, in the handler it process the data and can also pass back values to the ajax on the first page

(aspx page)

//Call the page method  
$.ajax({
    type: "POST",
    url: myHandler.ashx,
    contentType: "application/json;",
    data: dataObject,
    dataType: "json",
    success: ajaxCallSuccess,
    error: ajaxCallFailure
});

(handler page)

<%@ WebHandler Language="C#" Class="myHandler" %>
using System;
using System.Web;

public class myHandler : IHttpHandler 
{
    public void ProcessRequest(HttpContext context)
    {
        string strFname = string.empty;
        string strLname = string.empty;

        context.Response.ContentType = "application/json";

        if (!String.IsNullOrEmpty(context.Request.Form["FirstName"]))
        {
            strFname= Convert.ToString(context.Request.Form["FirstName"]);
        }
        if (!String.IsNullOrEmpty(context.Request.Form["LastName"]))
        {
            strLname = Convert.ToString(context.Request.Form["LastName"]);
        }

        context.Response.Write(HelloWorld(strFname, strLname));
    }

    public bool IsReusable
    {
        get { return false; }
    }

    public string HelloWorld(string fname, string lname)
    {
     return "Hello World, my name is " + fname + " " + lname;
    }
}

1 Comment

It doens't have to call a handler - it can easily call an aspx page and do the processing in Page_Load.
0

jQuery's $.ajax() function takes an error parameter which will be called if the request fails for any reason.

$.ajax({
   url: "someCommand.aspx",
   type: "POST",
   data: $("#myForm").serialize(),
   success: function(data){
       // display your data
   },
   error: funciton(){
       // handle error some way
   }
});

Or you can handle individual errors such as a 404, if you need to be specific, like this:

$.ajax({
  url: "someCommand.aspx",
  type: "POST",
  data: $("#myForm").serialize(),
  success: function(data){
     // display your data
  },
  statusCode: {
    404: function() {
      alert('page not found');
    }
  }
});

Best of Luck, Dave

2 Comments

Is this the only approach with jquery, i already use this, but if some exception occurs then this does not work, i also want if the receiving code could give some intellisense like it gives in code behind file.
If an exception occurs you can trap it in the error callback, ie: error: function() { alert('the aspx page on the other end threw an exception'); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.