0

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;
    }
2
  • Shouldn't you add the clicked_id as a parameter to PageMethods.javascriptTest(); ? Commented Aug 14, 2015 at 6:34
  • yeah forgot that, but thats not the issue!but thanks =) Commented Aug 14, 2015 at 6:37

1 Answer 1

3

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.