6

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; 
}
Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
altandogan
  • 1,245
  • 6
  • 21
  • 44

4 Answers4

5

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

Community
  • 1
  • 1
Manish Mishra
  • 12,163
  • 5
  • 35
  • 59
3

i am using generic Handler to solve this problem.

altandogan
  • 1,245
  • 6
  • 21
  • 44
1

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; 
}
4b0
  • 21,981
  • 30
  • 95
  • 142
1

I think you might be missing this attribute

   [System.Web.Script.Services.ScriptService]

Before you service class as

[System.Web.Script.Services.ScriptService]
public class TCSection: System.Web.Services.WebService
{

    [WebMethod]
    public static string InsertTCSection(string id)
    {
    }
 }

Or there may be one other reason that path of the webservice is not right.

शेखर
  • 17,412
  • 13
  • 61
  • 117