AJAX Operations with jQuery in ASP.NET


  jQuery has become a frequent use in web applications. This makes the library very attractive for programmers and designers, as it adds great visuals and effects to page design, and AJAX requests are simple and fast. I have recently heard a lot of questions about how to call a web service or PageMethod with jQuery, so I’ve also created a little application with jQuery about how different Ajax requests can be made in ASP.NET applications. In this direction in this article;

  • ASP.NET Ajax page
  • Parameter field on an ASP.NET Ajax page
  • Web service
  • pagemethod to

With jQuery we will examine how we can direct Asp.Net AJAX requests.

Let’s first introduce the ajax-named function that will perform AJAX requests in the jQuery library. The ajax function takes as parameters the url to be requested, the parameter data if any, the method of forwarding this data (such as GET, POST), and the type of data to be read as the request result. Other than these, it can also define the two functions which enable update on the page if the process is successful or to perform the necessary actions in case of error. All of the parametric values we consider here are not mandatory, only the parameters needed can be used according to the requirements. Below is a simple method call that can be used for AJAX requests.

Let’s see what the parameters mean:

type: The method of transmitting the request data. Like GET, POST.
url: Asynchronous request address.
success: The function to be executed if the request is successful. Here, the requested data is processed on the request page.

Here we can refer to the function definition of the success parameter as an anonymous method in C #. The result variable in the success method carries the data read in the AJAX request result. The $ (‘# result’) expression in this function is a jQuery selector expression that marks the HTML element containing the page id in our page. The resulting AJAX response will print the result of the named HTML element. Now let’s start by examining how we can manage these demands with an example.

Asking for QueryString on ASP.NET Page

In JQuery, you need to send a value to the parameter named data in ASP.NET ajax with QueryString as a parameter. There is a different spelling format for sending parameters here. data: ‘{“name”: “Taha”}’, the value is moved in brackets ({}). The parameter name and value are carried in the double quotation character (“), and the equality operator uses the two-point character (:). If more than one parameter is passed, the parameters must be separated by a comma character (,)..Below is an Ajax named page that shows how to execute ajax request by passing parameter via URL. Note that we need to change the OnClick property of our button element on the page to the name of the function below.

  • File:Home/Index.cshtml

If we run the page and enter a name for the element named text, we can see that it is sent to the Ajax.cshtml page with the name QueryString and the result is on our page.

Note: The escape () JavaScript function used above is used to resolve possible Turkish character problems.

  • File:Home/Ajax.cshtml

  • File:Controllers/HomeControllers.cs

I got the return value from the Get method in ajax Method

  • This takes request parameters only from the query string.
  • name is the parameter in the $ .ajax method

 

Demo

 

 

if you have question do not forget to write from the chat button next to it or from the comment

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *