Jquery ajax call to code behind method in user con

2019-05-24 11:56发布

问题:

Can we call User control code behind method using Jquery ajax ?

Thanks,

回答1:

You can't call a method directly in a user control using Jquery Ajax.

You can try one of the following approaches tho:

  • Set the URL to PageName.aspx?Method=YourMethod or maybe add some other restrictions so you know which user control should execute the method. Then in your user control you can check for the existance of your restrictions in the querystring, and execute the given method.

  • You can just use client callback to execute some method, if you need to do something async. in the GetCallbackResult in the page, you can find the control that caused the callback, and pass the request with its arguments to the control.



回答2:

"Can we call User control code behind method using Jquery ajax ?

If you mean using Web Methods, no. You can't directly access a method of a user control through an external query the way you can with a PageMethod. They are only supported at the the page (ascx) level.

Just use a WebService asmx for your functionality instead.



回答3:

No. You can't call code behinds of user controls from JQuery Ajax, but if you have an aspx page you can call it like this sample:

 function AddNew() {

        var lname = $("#<%# txtLNameAdd.ClientID %>").val(); //$("#txtFname").val(); // 
        var fname = $("#<%# txtfnameadd.ClientID %>").val(); //$("#txtLname").val(); // 
        var email = $("#<%# txtEmailAdd.ClientID %>").val();
        var address = $("#<%# txtAddressAdd.ClientID %>").val();
        var paramList = '{"fname":"' + fname + '","lname":"' + lname + '","email":"' + email    
         + '","address":"' + address + '"}';
        alert(paramList);
        $.ajax({
            type: "POST",
            url: "GetData.aspx/AddNewMember",
            dataType: "json",
            contentType: "application/json",
            processData: false,
            data: paramList,
            success: function (msg) {
                alert('success');
            },
            error: function (msg) {
                alert("error " + msg.responseText);
            }
        });
         return false;
     }

The code behind should be static and a [WebMethod] like this:

[WebMethod]
public static void AddNewMember(string fname, string lname, string email, string address)
{
    SqlConnection con = new SqlConnection(
        System.Configuration.ConfigurationManager
              .ConnectionStrings["RepeaterDBConnection"].ConnectionString);
    SqlCommand comm = new SqlCommand();
    try
    {
        comm.Connection = con;
        comm.CommandType = CommandType.StoredProcedure;
        comm.CommandText = "[dbo].[AddTeamMember]";
        comm.Parameters.Add(new SqlParameter("@psFname", SqlDbType.VarChar)).Value = fname;
        comm.Parameters.Add(new SqlParameter("@psLname", SqlDbType.VarChar)).Value = lname;
        comm.Parameters.Add(new SqlParameter("@psEmail", SqlDbType.VarChar)).Value = email;
        comm.Parameters.Add(new SqlParameter("@psAddress", SqlDbType.VarChar)).Value = address;
        con.Open();
        comm.ExecuteNonQuery();
    }
    catch (SqlException Ex)
    {
        // write any exception 
    }
    finally
    {
        con.Close();
    }
}

I didn't try it with a user control before, but you can try it. Put the Ajax method on the page containing the control and call the WebMethod from the user control.

I think it should work because all the files are going to be merged.