Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use jQuery ajax with asp.net user controls?

I want to use ajax with asp.net user control,

    $.ajax({
        type: "POST",
        url: "*TCSection.ascx/InsertTCSection",
        data: "{id:'" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var URL = msg.d;
            alert(URL);
        });

.cs code

[WebMethod]
public static string InsertTCSection(string id)
{
    string result = "deneme";
    return result; 
}
like image 770
altandogan Avatar asked Feb 26 '13 11:02

altandogan


People also ask

Can AJAX work with ASP NET?

Calling Web Services with ASP.NET AJAXAll of this can be done without resorting to postback operations. While the ASP.NET AJAX UpdatePanel control provides a simple way to AJAX enable any ASP.NET page, there may be times when you need to dynamically access data on the server without using an UpdatePanel.

Can jQuery and AJAX be used together?

jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!

Can we use AJAX in MVC?

Web. Mvc. Ajax namespaces can be combined with JavaScript and MVC partial views to create flexible interactive web pages with minimal code. When using these resources, developers should be aware of a few techniques necessary to create effective code.

What is the use of AJAX () method?

The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.


3 Answers

You cannot call a method kept inside a usercontrol through jquery. because .ascx controls don't represent real url.

They are meant to be embedded in some ASP.NET page, hence they don't exist at runtime. what you might do is, to create a separate service and place your method there. like webservices.

see this, this and many others

like image 89
Manish Mishra Avatar answered Oct 22 '22 19:10

Manish Mishra


i am using generic Handler to solve this problem.

like image 39
altandogan Avatar answered Oct 22 '22 19:10

altandogan


Try:

 $.ajax({
        type: "POST",
        url: "*TCSection.ascx/InsertTCSection",
        data: JSON2.stringify({ id: id}, //Your 2nd id is passing value but i dont know where its come from
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var URL = msg.d;
            alert(URL);
             }
         )};

and in cs:

[WebMethod]
public static string InsertTCSection(string id)
{
    string result="deneme";
    return result; 
}
like image 39
4b0 Avatar answered Oct 22 '22 20:10

4b0