User-719153870 posted
Hi gordon1221,
As far as i kown, if you want to use ajax to send a parameter to the back code and get some data from that, you won't need to use a method with EvenArgs.
A simple demo using ajax and a static method might help you understand what does that mean:
ASPX:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-3.3.1.min.js"></script>
<script>
$(function () {
$('#btninsert').click(function () {
var sumIt = "6";
$.ajax({
type: "POST",
url: "AjaxInsertDemo.aspx/ItemInserting",
data: "{'sumIT':'" + sumIt + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert('success');
},
error: function (msg) {
alert('fail');
}
});
});
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="btninsert" value="insert" />
</div>
</form>
</body>
</html>
.CS:
[WebMethod]
public static void ItemInserting(string sumIT)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DatabaseTestPool;Integrated Security=True");
con.Open();
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "Insert into Options values('" + sumIT + "','D1')";
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw (ex);
}
}
For more information about using ajax and webmethod, please refer to
this artical.
Best Regard,
Yang Shen