Sun Nov 09, 2008 6:39 am
using System.Web.Script.Services;
...
[ScriptService]
public class DataAccess : System.Web.Services.WebService
{ ...
[WebMethod]
public string FetchOrders(string key)
{
return LoadData(key);;
}
private string LoadData(string key)
{
DataSet ds = new DataSet();
ds.ReadXml(this.Context.Server.MapPath("App_Data\\northwind.xml"));
ds.Tables["Orders"].DefaultView.RowFilter = "customerid = '" + key + "'";
return RenderResultTable((ds.Tables["Orders"].DefaultView));
}
private string RenderResultTable(DataView rdr)
{
StringBuilder str = new StringBuilder();
str.AppendLine("<table cellspacing=\"0\" cellborder=\"=0\"
cellpadding=\"5\">");
str.AppendLine("<tr><td><b>Order Date</b></td><td><b>Order ID</b>
</td></tr>");
foreach(DataRowView drv in rdr)
{
str.AppendLine("<tr>");
str.AppendFormat("<td>{0}</td> <td>{1}</td>", drv["OrderDate"],
drv["OrderID"]);
str.AppendLine("</tr>");
}
str.AppendLine("</table>");
return str.ToString();
}
var cacheData = new Array(); // This is our cache
var currSel = null;
var seedData = "0BFA4D6B-DD18-48aa-A926-B9FD80BFA5B7";
// Prime a cache item with a unique initial value
function btnFetch_onclick()
{
currSel = document.getElementById("dlCustomers").value;
var status = document.getElementById("divStatus");
//var svc = new DataAccess();
if(cacheData[currSel]==null)
{
DataAccess.FetchOrders(currSel,OnCompleteCallback,OnErrorCallback,
OnTimeOutCallback);
cacheData[currSel] = seedData;
status.innerHTML = "[Live Result]";
}
else
{
status.innerHTML = "[Cached Result]";
var cacheobject = FetchDataFromCache(currSel);
RenderData(cacheobject);
}
document.getElementById("dlCustomers").focus();
}
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Script.Services;
using System.Text;
using System.Threading;
/// <summary>
/// Summary description for DataAccess
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class DataAccess : System.Web.Services.WebService
{
public DataAccess ()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string FetchOrders(string key)
{
//SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
////SqlCommand cmd = new SqlCommand("select CONVERT(CHAR(10), orderdate, 101) as OrderDate, OrderID from orders where customerid = @custid", conn);
//SqlCommand cmd = new SqlCommand("select CONVERT(CHAR(10), orderdate, 101) as OrderDate,customerid, OrderID from orders", conn);
//SqlParameter parm = new SqlParameter("@custid", key);
//cmd.Parameters.Add(parm);
//conn.Open();
//SqlDataReader rdr = cmd.ExecuteReader();
//DataSet ds = new DataSet();
//ds.Load(rdr, LoadOption.OverwriteChanges, new string[] { "Orders" });
//ds.WriteXml("c:\\orders.xml");
////return FormatHTMLTable(rdr);
//return "hello";
string results = LoadData(key);
return results;
}
private string RenderResultTable(DataView rdr)
{
StringBuilder str = new StringBuilder();
str.AppendLine("<table cellspacing=\"0\" cellborder=\"=0\" cellpadding=\"5\">");
str.AppendLine("<tr><td><b>Order Date</b></td><td><b>Order ID</b></td></tr>");
foreach(DataRowView drv in rdr)
{
str.AppendLine("<tr>");
str.AppendFormat("<td>{0}</td> <td>{1}</td>", drv["OrderDate"], drv["OrderID"]);
str.AppendLine("</tr>");
}
str.AppendLine("</table>");
return str.ToString();
}
private string LoadData(string key)
{
DataSet ds = new DataSet();
ds.ReadXml(this.Context.Server.MapPath("App_Data\\northwind.xml"));
//DataView dv = ds.Tables["Orders"].DefaultView;
//dv.RowFilter = "customerid = '" + key + "'";
ds.Tables["Orders"].DefaultView.RowFilter = "customerid = '" + key + "'";
return RenderResultTable((ds.Tables["Orders"].DefaultView));
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Client-side Caching using AJAX</title>
<link href="Style.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript">
// <!CDATA[
var cacheData = new Array();
var currSel = null;
var seedData = "0BFA4D6B-DD18-48aa-A926-B9FD80BFA5B7";
function btnFetch_onclick()
{
currSel = document.getElementById("dlCustomers").value;
var status = document.getElementById("divStatus");
//var svc = new DataAccess();
if(cacheData[currSel]==null)
{
DataAccess.FetchOrders(currSel,OnCompleteCallback,OnErrorCallback, OnTimeOutCallback);
cacheData[currSel] = seedData;
status.innerHTML = "[Live Result]";
}
else
{
status.innerHTML = "[Cached Result]";
var cacheobject = FetchDataFromCache(currSel);
RenderData(cacheobject);
}
document.getElementById("dlCustomers").focus();
}
function FetchDataFromCache(objectkey)
{
return cacheData[objectkey];
}
function RenderData(data)
{
var cndiv = document.getElementById("ajaxContent");
cndiv.style.display = "";
cndiv.innerHTML = String(data);
}
function OnCompleteCallback(retResult)
{
if(cacheData[currSel]==seedData)
{
cacheData[currSel]=retResult;
}
RenderData(retResult);
}
function OnTimeOutCallback(retResult)
{
alert ("AJAX Callback Timeout occurred.");
}
function OnErrorCallback(retResult)
{
alert ("AJAX Callback Error occurred.");
}
function OnBodyLoad()
{
document.getElementById("dlCustomers").focus();
}
function OnKeyPress()
{
if(event.keyCode == 13)
btnFetch_onclick();
}
// ]]>
</script>
</head>
<body style="text-align:center;" onload="OnBodyLoad()" onkeypress="return OnKeyPress();">
<form id="form1" style="text-align:center;" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="0" ScriptLoadTimeout="0" ScriptMode="Release" >
<Services>
<asp:ServiceReference Path="DataAccess.asmx" />
</Services>
</asp:ScriptManager>
<div id="MainContainer" style="float:left; width: 593px; height: 252px;" class="Container">
<h3 style="text-align: center">Using Associative Arrays for Client-side Caching using AJAX & Javascript</h3>
<div id="MainContent" style="float:left;width:200px;text-align:left;">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Select a Customer:"></asp:Label>
<asp:DropDownList ID="dlCustomers" runat="server" Width="198px">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
<input type="button" style="margin-top:10px; height: 24px; width: 72px;" value="Fetch" id="btnFetch" onclick="return btnFetch_onclick()" />
<br />
<span id="divStatus" style="width: 198px; height: 19px;color:Navy;"></span><br />
</div>
<asp:Panel ID="ajaxContent" runat="server" CssClass="DivTable">
</asp:Panel>
</div>
<cc1:DropShadowExtender ID="DropShadowExtender1" runat="server" Opacity="0.2" TargetControlID="ajaxContent"
Width="3">
</cc1:DropShadowExtender>
</form>
</body>
</html>
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.