简体   繁体   English

从javascript调用方法后面的代码?

[英]Calling code behind method from javascript?

I have method in code behind: 我在代码中有方法:

protected void myMethod()
    {
        Literal1.Text = System.DateTime.Now.ToString();
    }

I need to call this method every second. 我需要每秒调用这个方法。 I found that i should use javascipt, here's my scipt: 我发现我应该使用javascipt,这是我的scipt:

<script>
   function test() {
      // ???
   }
   setInterval(test, 1000);
</script>

but i don't know how to call myMethod from javascript? 但我不知道如何从JavaScript调用myMethod?

Use AJAX. 使用AJAX。 https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started This will allow you to call a designated server-side method from the client. https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started这将允许您从客户端调用指定的服务器端方法。

From the link above: 从上面的链接:

function makeRequest(url) {
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
      httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
      try {
        httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch (e) {
        try {
          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) {}
      }
    }

    if (!httpRequest) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
    }
    httpRequest.onreadystatechange = alertContents;
    httpRequest.open('GET', url);
    httpRequest.send();
  }

  function alertContents() {
    if (httpRequest.readyState === 4) {
      if (httpRequest.status === 200) {
        alert(httpRequest.responseText);
      } else {
        alert('There was a problem with the request.');
      }
    }
  }
})();

You'll have to decorate your server-side method with the [WebMethod] attribute and declare it as public and static. 您必须使用[WebMethod]属性修饰服务器端方法,并将其声明为public和static。 http://msdn.microsoft.com/en-us/library/byxd99hx(v=vs.90).aspx http://msdn.microsoft.com/en-us/library/byxd99hx(v=vs.90).aspx

From MSDN: 来自MSDN:

[System.Web.Services.WebMethod(EnableSession=true)]
public int GetNumberOfConversions()
{
   return (int) Session["Conversions"];
}

It's that easy. 就这么简单。

I have to question calling a method from client to server every second, however. 但是,我必须质疑每秒从客户端到服务器调用一个方法。 You can handle that date business in JavaScript alone. 您可以单独使用JavaScript处理该日期业务。 Making a round-trip is not going to work out well for you, I'm betting. 对你来说,往返旅行并不顺利,我敢打赌。

If you want to avoid the server round-trip, which you might because of network delay, you can use javascript like so 如果你想避免服务器往返,这可能是因为网络延迟,你可以像这样使用javascript

var now = new Date();
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");// Saturday, June 9th, 2007, 5:46:21 PM

Check out http://blog.stevenlevithan.com/archives/date-time-format to see what other type of date formats are possible. 查看http://blog.stevenlevithan.com/archives/date-time-format以查看可能的其他类型的日期格式。

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

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