Browser History ASP.Net AJAX: Microsoft.Web.Preview
- by Narendra Tiwari
I remember in 2006 we were working on a portal for our client Venetian, Las Vegas and the portal is full of AJAX features. One of my friend facing a challange to retain browser history with all AJAX operation. In terms of user experience it is an important aspect which could not be avoided in that scenario. Well that time we have made some workarounds to achieve the same but that may not be the perfect solution.
Ok.. Now with Microsoft AJAX there are a lot of such features can be achieved with optimum efficiency. Microsoft AJAX has grown its features over the past few years. Microsoft.Web.Preview.dll is an addon in conjunction with ASP.Net AJAX. It contains a control named "History" for that purpose.
Source code:-
http://download.microsoft.com/download/8/3/1/831ffcd7-c571-4075-b8fa-6ff678794f60/CS-ASP-ASPBrowserHistoryinAJAX_cs.zip
Below is a small sample to demonstrate the control.
1/ Get dll from the above source code bin, and add reference to your web application.
2/ Rightclick on toolbox panel and Choose Item, browse assembly. now you will be able to see History control.
3/ Add below section group in web.config under <configSections>
<sectionGroup name="microsoft.web.preview" type="Microsoft.Web.Preview.Configuration.PreviewSectionGroup, Microsoft.Web.Preview">
<section name="search" type="Microsoft.Web.Preview.Configuration.SearchSection, Microsoft.Web.Preview" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="searchSiteMap" type="Microsoft.Web.Preview.Configuration.SearchSiteMapSection, Microsoft.Web.Preview" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="diagnostics" type="Microsoft.Web.Preview.Configuration.DiagnosticsSection, Microsoft.Web.Preview" requirePermission="false" allowDefinition="MachineToApplication"/>
</sectionGroup>
4/ Now create a simple webpage a textbox (txt1), button (btn1) in an updatePanel with History control (History1). We will fill in text box and post the fom by clicking button a few times then verify if the browse history is retained. Remember button and textbox must be inside UpdatePanel and History control outside the UpdatePanel.
<%@Page Language="C#" AutoEventWireup="true" CodeFile="History.aspx.cs" Inherits="History" %>
<%@ Register Assembly="Microsoft.Web.Preview" Namespace="Microsoft.Web.Preview.UI.Controls" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
<div>
<cc1:History ID="History1" runat="server" OnNavigate="History1_Navigate">
</cc1:History>
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox><br />
<asp:Button ID="btn1" runat="server" Text="Test" OnClick="btn1_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="History1" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
5/ Below code to add the textbox value in history everytime we post back using btn1 click.
protected void btn1_Click(object sender, EventArgs e)
{
History1.AddHistoryPoint("txtState",txt1.Text);
}
6/ and finally Navigate event of History control
protected void History1_Navigate(object sender, Microsoft.Web.Preview.UI.Controls.HistoryEventArgs args)
{
string strState = string.Empty;
if (args.State.ContainsKey("txtState"))
{
strState = (string)args.State["txtState"];
}
txt1.Text = strState;
}
Now all set to go :)
Reference:
http://www.dotnetglobe.com/2008/08/using-asp.html