简体   繁体   English

从javascript函数调用C#方法背后的代码时出错

[英]error in calling code behind C# method from javascript function

I have the following java script function 我有以下的Java脚本功能

<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. 而且我想从javascript函数调用setDone方法并传递taskID而不是123,但是我现在不知道该怎么做。 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 而且我不知道为什么服务器在页面加载后没有调用taskDone“ JavaScript”函数的情况下自动运行此脚本脚本'<%= setDone(123)%>'

Few things. 一些事情。

First, you need to decorare your method with the WebMethod attribute and also make it static . 首先,您需要使用WebMethod属性来修饰您的方法,并将其设置为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 : 现在,确保您具有ScriptManager

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

Then in JavaScript, you call it by doing: 然后在JavaScript中,您可以通过以下方式调用它:

PageMethods.setDone(taskID, onSuccessMethod,onFailMethod);

Whilst also having success and fail methods (passed in above): 同时也有成功和失败方法(上面已传递):

function onSuccessMethod(success) {
    //query success
}

function onFailMethod() {
    //check failure
}

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. 所有服务器内联代码(例如<%=setDone(123)%>都是在页面生成期间执行的,它在JavaScript中的位置仅仅是因为您将其放置在页面中。

You can use inline code to pass server side data to JavaScript in this fashion. 您可以使用内联代码以这种方式将服务器端数据传递 JavaScript。

If you want JavaScript to execute server side code, you can use several techniques. 如果希望JavaScript执行服务器端代码,则可以使用多种技术。 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. 就像mattyTommo所描述的WebMethods一样,您可以在输入框中输入一个值,然后为服务器端按钮单击按钮并让服务器端代码读取输入框的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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