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; 
}
3
  • Are you getting some error? Commented Feb 26, 2013 at 11:42
  • i am adding breakpoint at server side function and it is not hit Commented Feb 26, 2013 at 11:46
  • you cannot call a method kept inside UserControl through jquery, as at runtime there is no such resource as .ascx, it is merged with the page onto which it is kept Commented Feb 26, 2013 at 11:52

4 Answers 4

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

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

Comments

3

i am using generic Handler to solve this problem.

Comments

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; 
}

1 Comment

id parameter not important i want to learn about ajax with .ascx. is any way to do this ?
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.

Comments

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.