我在按钮单击时传递了JavaScript字符串数组,在按钮单击时,dataArray存储了表中第一个元素的字符串值,在该表中已选择了某些行,stringify
然后将其作为JSON并调用Ajax函数来发送数据到函数DeleteStudent后面的代码中。
我在按钮上单击时调用的JavaScript函数:
$('#deleteStudent').click(function () {
var dataArr = [];
$.each($("#StudentTable tr.selected"), function () {
dataArr.push($(this).find('td').eq(0).text());
});
var StudentList = JSON.stringify(dataArr);
$.ajax({
type: "POST",
url: "ViewStudents.aspx/DeleteStudent",
contentType: "application/json; charset=utf-8",
data: { Students: dataArr },
dataType: "json",
traditional: true,
success: function (result) {
alert('Yay! It worked!');
},
error: function (result) {
alert('Oh no :( : '+result);
}
});
console.log(StudentList);
});
dataArray看起来像这样
["10363","10364","10366"]
功能背后的代码:
[WebMethod]
public static void DeleteStudent(string[] Students)
{
Console.WriteLine("Reached CS");
string[] a =Students;
for (int i = 0; i < a.Length; i++)
{
string admissionNumber=a[i];
using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
{
using (MySqlCommand deleteStudent = new MySqlCommand())
{
deleteStudent.CommandType = CommandType.Text;
deleteStudent.Connection = conn;
deleteStudent.CommandText = "DELETE FROM validstudents WHERE admissionNumber = @admissionNumber ";
deleteStudent.Parameters.AddWithValue("@admissionNumber", admissionNumber);
conn.Open();
deleteStudent.ExecuteNonQuery();
conn.Close();
}
}
}
}
它提供了500个内部服务器
始终将JSON字符串化后再发送到 WebMethod
data: JSON.stringify({ Students: dataArr })
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句