简体   繁体   中英

Asp.net Call Code Behind with JavaScript

since i didn´t find any solution that helped me, i thought i asked. I need a JavaScriptfunction with calls a method in my code-behind, and since i´m really new to this, i dont understand what am i doing wrong. On my Master Page (Site.Master) i enabled PageMethods:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />

And in the Content of my Page i have put the Script:

        function reply_click(clicked_id) {
            alert(clicked_id);
            PageMethods.javascriptTest(clicked_id);

And now my method in the code-behind:

    [WebMethod]
    public void javascriptTest(int buttonID)
    {

        Test2.Text += buttonID + "***";

        // Use the ID for DB access

    }

I need that ID for later, when i have to do stuff in my database, but i´m always getting PageMethods undefined errors and i dont know why :/

EDIT: The Solution was indeed to make the WebMethod static, i just made a workaround, so i could use all the details i need for my db access

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);
        }

Code-Behind:

    [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;
    }

Your javascriptTest method needs to be static

Try This:

[System.Web.Services.WebMethod]
public static void javascriptTest(int buttonID)
{
      Test2.Text += buttonID + "***";
      // Use the ID for DB access
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM