Home > ASP.Net, Javascript > Use WebService with Javascript to Get Auto DateTime

Use WebService with Javascript to Get Auto DateTime


Hi all,

try this example to Use WebService with Javascript to Get Auto DateTime

1) open vs2008 and create a new web site and add new WebService name it “WebService.asmx” and add this code :-


using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
 [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

public WebService () {

//Uncomment the following line if using designed components
 //InitializeComponent();
 }

[WebMethod]
 public string GetTime()
 {
 return DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss");
 }

}

2) Add new Web Page and in ASPX Design add this :-


<head runat="server">
 <title>Untitled Page</title>
 <script type="text/javascript" language="javascript">
 var interval = 0;
 function pageLoad() {
 callWebService();
 interval = window.setInterval("callWebService()", 3000);
 }

function callWebService() {
 WebService.GetTime(successCallback, failCallback);
 }

function successCallback(result) {
 var divOutput = document.getElementById("divOutput");
 divOutput.innerHTML = result;
 }

function failCallback(error) {
 alert('Error: ' + error.get_exceptionType());
 }

 </script>
</head>
<body>
 <form id="form1" runat="server">
 <asp:ScriptManager runat="server" ID="scriptManager1">
 <Services>
 <asp:ServiceReference Path="~/WebService.asmx" />
 </Services>
 </asp:ScriptManager>
 <div id="divOutput">
 </div>
 </form>
</body>

then run your page you will find time change every 3 sec this is because i call the result from webservice

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Javascript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: