0

I am trying to call the method insert which is in Edit.aspx.cs but it is not calling

    <script src="jquery-2.0.2.js"></script>

   <script language="javascript">
               function insert() {

              $.ajax({
              type: "POST",
              url: "Edituser.aspx.cs/insert",
               success: function () { alert('success'); },
               error: function () { alert('error'); }

                });


              }


</script>

 <input type="button" id="Button1" style="display: none" onclick="changecourse(); insert();"  value="add" />

My codebehind

    public void insert()
    {
        string a = Hidden1.Value;
        string UserId = Convert.ToString(Session["LoginId"]);
        con.Open();
        SqlCommand cmd = new SqlCommand("INSERT INTO UserDepot (UserId,DepotId)" +
            "VALUES ('" + UserId + "','" + a + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
1
  • You certainly can't issue a request to a .cs file like that... cfr Milind's answer here under. Commented Jan 24, 2014 at 9:38

2 Answers 2

4

For it to work, ensure that the method location set in url is correct and that the method is public and static and that is has a [WebMethod] attribute added such as:

[WebMethod]
public static void doAll()
{
    //do something
}

if the url is "/Default.aspx/insert" then your method should look like this:

[WebMethod]
public static void insert()
{
    //do something
}
Sign up to request clarification or add additional context in comments.

3 Comments

I didn't know you could put WebMethods in a code behind page - interesting, do you consider it good practice or would you use a separate WS/WCF?
WebMethod indicates "the method exposed as part of the XML Web service." and it enables the method to be called through the web service. So i believe you should be using this
I'd use ASP.NET Web API if you are considering WCF. It's THAT much easier... But if you only have a few methods it would be overkill.
0

Send a QueryString to edit user to aspx

$.ajax({
          type: "POST",
          url: "Edituser.aspx?run=add",
           success: function () { alert('success'); },
           error: function () { alert('error'); }

            });

then put a if to pageload of Edituser.aspx

if (Request.QueryString["run"] == "add")
            {
               insert();//your Function Name
            }

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.