Last post Oct 13, 2016 08:46 AM by Brando ZWZ
Member
10 Points
46 Posts
Oct 13, 2016 04:08 AM|kiang_yit|LINK
<script type="text/javascript"> $("#myinput1").on('input', function () { if ($(this).val().length >= 8) { $.ajax({ type: "POST", url: "Default.aspx/fillfields", contentType: "application/json; charset=utf-8", data: '{"input":"' + $("#myinput1").val() + '"}', dataType: "json", }) $('#myinput1').val(""); } }); </script> <input autofocus="autofocus" id="myinput1" /> [System.Web.Services.WebMethod] private void fillfields(string input) { string constr = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString; using (SqlConnection connection = new SqlConnection(constr)) { connection.Open(); using (SqlCommand command = new SqlCommand("TMS_INSERT", connection)) { command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("@EMP_ID", input); SqlDataReader reader = command.ExecuteReader(); } //myinput1.Value = string.Empty; connection.Close(); } }
its fail to call c# function.tried to solve but failed.
Star
9011 Points
2408 Posts
Oct 13, 2016 04:22 AM|Lokesh B R|LINK
Hi,
try this code
<script type="text/javascript"> $("#myinput1").on('input', function () { if ($(this).val().length >= 8) { $.ajax({ type: "POST", url: "Default.aspx/fillfields", contentType: "application/json; charset=utf-8", data: '{input: "' + $("#myinput1").val() + '"}', dataType: "json", }) $('#myinput1').val(""); } }); </script> <input autofocus="autofocus" id="myinput1" /> [System.Web.Services.WebMethod] public void fillfields(string input) { string constr = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString; using (SqlConnection connection = new SqlConnection(constr)) { connection.Open(); using (SqlCommand command = new SqlCommand("TMS_INSERT", connection)) { command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("@EMP_ID", input); SqlDataReader reader = command.ExecuteReader(); } //myinput1.Value = string.Empty; connection.Close(); } }
Oct 13, 2016 04:24 AM|kiang_yit|LINK
not working too....
8921 Points
2719 Posts
Oct 13, 2016 08:46 AM|Brando ZWZ|LINK
Hi kiang_yit,
kiang_yit not working too....
As far as I know, if you want to call webmethod in code-behind.
You must set this method public and static.
Besides, I suggest you could create a success function in ajax to know the ajax execute successfully.
More details, you could refer to follow codes:
<script> $(function () { $("#myinput1").on('input', function () { if ($(this).val().length >= 8) { $.ajax({ type: "POST", url: "AjaxWithGridviewWebmehtod.aspx/fillfields", contentType: "application/json; charset=utf-8", data: '{input: "' + $(this).val() + '" }', dataType: "json", success: function (data) { alert("Success"); }, error:function() { alert('some error occurred'); } }) $('#myinput1').val(""); } }); }); </script> <input autofocus="autofocus" id="myinput1" />
Webmethod:
[System.Web.Services.WebMethod] public static void fillfields(string input) { int i = 0; string constr = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString; using (SqlConnection connection = new SqlConnection(constr)) { connection.Open(); using (SqlCommand command = new SqlCommand("TMS_INSERT", connection)) { command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("@EMP_ID", input); SqlDataReader reader = command.ExecuteReader(); } //myinput1.Value = string.Empty; connection.Close(); } }
Link:http://www.aspsnippets.com/Articles/Send-Pass-multiple-parameters-to-WebMethod-in-jQuery-AJAX-POST-in-ASPNet.aspx
Best Regards,
Brando
Member
10 Points
46 Posts
jquery call c# function
Oct 13, 2016 04:08 AM|kiang_yit|LINK
its fail to call c# function.tried to solve but failed.
Star
9011 Points
2408 Posts
Re: jquery call c# function
Oct 13, 2016 04:22 AM|Lokesh B R|LINK
Hi,
try this code
Member
10 Points
46 Posts
Re: jquery call c# function
Oct 13, 2016 04:24 AM|kiang_yit|LINK
not working too....
Star
8921 Points
2719 Posts
Re: jquery call c# function
Oct 13, 2016 08:46 AM|Brando ZWZ|LINK
Hi kiang_yit,
As far as I know, if you want to call webmethod in code-behind.
You must set this method public and static.
Besides, I suggest you could create a success function in ajax to know the ajax execute successfully.
More details, you could refer to follow codes:
Webmethod:
Link:http://www.aspsnippets.com/Articles/Send-Pass-multiple-parameters-to-WebMethod-in-jQuery-AJAX-POST-in-ASPNet.aspx
Best Regards,
Brando