Tuesday, March 17, 2015

Simple Ajax

First we have a simple web service

here is the interface

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ItimeService" in both code and config file together.
[ServiceContract]
public interface ItimeService
{
 [OperationContract]
 string GetCurrentTime();
}

Here is the extremely simple service that implements that interface

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "timeService" in code, svc and config file together.
public class timeService : ItimeService
{
 

    public string GetCurrentTime()
    {
        return DateTime.Now.ToLongTimeString();
    }
}

You want to run this and keep it running. Start a second instance of Visual Studio to create the client. Create the reference to the service.

here is the web form for Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Time of Day</h1>
        <asp:Image ID="Image1" runat="server" ImageUrl="~/the-persistence-of-memory-4.jpg"/>
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
          <ContentTemplate>
              <asp:Timer ID="Timer1" runat="server" Enabled="true"   OnTick="Timer1_Tick" Interval="1000" ></asp:Timer>
              <asp:Label ID="lblTime" runat="server" Text="Label"></asp:Label>
          </ContentTemplate>
        </asp:UpdatePanel>
        
    </div>
    </form>
</body>
</html>

Here is the default.aspx.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        
       
        
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
       
        TimeServiceReference.ItimeServiceClient tsr = new TimeServiceReference.ItimeServiceClient();
        lblTime.Text = tsr.GetCurrentTime();
    }
   
}

No comments:

Post a Comment