[英]Asp.net Call Code Behind with JavaScript
因为我没有找到任何对我有帮助的解决方案,所以我以为我问。 我需要一个JavaScript函数,该函数在我的代码背后调用一个方法,并且由于我真的很陌生,所以我不明白我在做什么错。 在我的母版页(Site.Master)上,我启用了PageMethods:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
在页面的内容中,我添加了脚本:
function reply_click(clicked_id) {
alert(clicked_id);
PageMethods.javascriptTest(clicked_id);
现在,我的方法在代码背后:
[WebMethod]
public void javascriptTest(int buttonID)
{
Test2.Text += buttonID + "***";
// Use the ID for DB access
}
当我不得不在数据库中做一些事情时,我需要该ID以便以后使用,但是我总是会收到PageMethods未定义的错误,而且我不知道为什么:/
编辑:解决方案的确是使WebMethod静态化,我只是做了一个解决方法,因此我可以使用数据库访问所需的所有详细信息
JavaScript:
function reply_click(clicked_id, clicked_name) {
// Schulungsdaten holen
var grid = document.getElementById("<%= SignGridView.ClientID %>");
var row = grid.rows[0];
var appointmentData = row.cells[clicked_id].innerText;
// Userdaten holen
var userID = document.getElementById("<%= UserData.ClientID %>").innerText;
PageMethods.javaScriptUserSignIn(appointmentData, userID, clicked_name, OnSucceeded, OnFailed);
location.reload();
}
function OnSucceeded(response) {
alert(response);
}
function OnFailed(error) {
alert(error);
}
代码隐藏:
[WebMethod]
public static string javaScriptUserSignIn(string appointmentData, string userID, string status)
{
string result = "";
SignIn sign = new SignIn();
result = sign.SignToTraining(appointmentData, userID, status);
return result;
}
您的javascriptTest方法必须是静态的
尝试这个:
[System.Web.Services.WebMethod]
public static void javascriptTest(int buttonID)
{
Test2.Text += buttonID + "***";
// Use the ID for DB access
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.