0

I have the following java script function

<script type="text/javascript">
          function taskDone(taskID) {

              alert(taskID);

              var btn = document.getElementById('btn-taskDone-' + taskID);
              var icon = document.getElementById('task-icon-' + taskID);

              btn.style.color = '#8F9199';
              btn.onclick = "";
              btn.style.cursor = 'default';
              icon.src = "./../Images/Icons/doneTask.png";                  

              '<%=setDone(123)%>'

          }

</script>

and the following code behind method

 public Boolean setDone(int taskID)
    {
        BO.taskDao myTask = new BO.taskDao();
        Boolean success = myTask.setDone(1, taskID);
        return success;
    }

and i want to call setDone method from javascript function and passing taskID instead of 123, but i don't now how to do that. and i didn't know why server automatically run this line of script '<%=setDone(123)%>' when the page loaded and without call taskDone "JavaScript" function

1
  • you prefer using ajax or doing a postback? Commented Oct 2, 2013 at 10:50

2 Answers 2

2

Few things.

First, you need to decorare your method with the WebMethod attribute and also make it static. Try this:

[WebMethod]
public static Boolean setDone(int taskID)
{
    BO.taskDao myTask = new BO.taskDao();
    Boolean success = myTask.setDone(1, taskID);
    return success;
}

Now, make sure you have a ScriptManager:

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

Then in JavaScript, you call it by doing:

PageMethods.setDone(taskID, onSuccessMethod,onFailMethod);

Whilst also having success and fail methods (passed in above):

function onSuccessMethod(success) {
    //query success
}

function onFailMethod() {
    //check failure
}
Sign up to request clarification or add additional context in comments.

Comments

1

All server inline code such as <%=setDone(123)%> is executed during generation of the page, and it's position in JavaScript is merely because you placed it there.

You can use inline code to pass server side data to JavaScript in this fashion.

If you want JavaScript to execute server side code, you can use several techniques. As well as WebMethods described by mattyTommo you can put a value in a input box, and then call a button click for a server side button and have the server side code read the content of the input box.

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.