Is there a remended way to make an AJAX call within a WebForms application?
I know there is the built-in ASP.NET AJAX ponents, but they seem a bit heavy. I'm used to doing this in MVC and it seems very clean.
I can use Page Methods, but they require that my method is static, which makes it more difficult to access my database, etc.
I assume I can also just use jQuery to make the call, although attempts at this have failed in the past, usually due to problems with the way data was returned (JSON, etc.).
My goal is get pass a string fragment and get back a list of items that match that fragment. Speed would be nice. Can I get some remendations?
Is there a remended way to make an AJAX call within a WebForms application?
I know there is the built-in ASP.NET AJAX ponents, but they seem a bit heavy. I'm used to doing this in MVC and it seems very clean.
I can use Page Methods, but they require that my method is static, which makes it more difficult to access my database, etc.
I assume I can also just use jQuery to make the call, although attempts at this have failed in the past, usually due to problems with the way data was returned (JSON, etc.).
My goal is get pass a string fragment and get back a list of items that match that fragment. Speed would be nice. Can I get some remendations?
Use an HTTP handler (.ashx), this will give the page instance and flexibility of it being script callable via jQuery .ajax()
method, like this:
$.ajax({
url: "Handler/MyHandler.ashx",
contentType: "application/json; charset=utf-8",
data: { 'Id': '10000', 'Type': 'Employee' },
success: OnSuccess,
error: OnFail
});
function OnSuccess() {
// Do whatever needs to happen on success here
}
function OnFail() {
// Do whatever needs to happen on failure here
}
I always use JQuery AJAX calls:
$.ajax({
url: 'your url',
headers: { headertitle: headerdata},
cache: false,
success: function(data) {
//success function. Returned data is stored in the data variable.
}
});
Let me know if you have further questions.
I'll throw my vote in with the jQuery AJAX calls.
But in my years doing WebForms, calling code-behind file methods wasn't the preferred way. I'd expose the functionality as a Web Service. You can do it as WCF or WebAPI (ing from MVC, WebAPI may be a better fit for your expertise).
If it's a one-off function call to do the AutoComplete functionality (I'm assuming based on the problem you describe), you can probably get by with calls to code-behind methods, and you could use the AjaxControlToolkit's AutoCompleter control. But if you notice you're doing more and more calls via AJAX, you need to consider putting your service calls into a true Web Service.