Search This Blog

Sunday, November 28, 2010

Output caching by using VaryByParam attribute

you can able to cache multiple versions of the same page by varying the query string value.
Example:
1.Create a page called City.aspx with the below code.
2.Replace your html body code with the below code
<form id="form1" runat="server">
<div>
<asp:label id="lblCity" text="Select a city:" runat="server">
<asp:listbox id="lbCity" runat="server" autopostback="True" onselectedindexchanged="lbCity_SelectedIndexChanged">
<asp:listitem text="Bangalore" value="1">
<asp:listitem text="Hyderabad" value="2">
<asp:listitem text="Chennai" value="3">
</asp:listitem></asp:listitem></asp:listitem></asp:listbox>
</asp:label></div>
</form>3.Create selected index changed event for List Box with the name lbCity_SelectedIndexChanged in the City.aspx.cs.
protected void lbCity_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("VaryByParam.aspx?city=" + lbCity.SelectedItem.Text);
}

4.Create an another page called VaryByParamTest.aspx
5.In the html code of the VaryByParamTest.aspx page copy paste the below line of code immediately after the "@ Page" directive line of code

6.In the page load of the VaryByParamTest.aspx.cs write below code.
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToString());
}
Testing the Code:
1.Click on F5 by setting the City.aspx as the start page.
2.Select any item in the list box ( for example You select Bangalore )
3.Observe the server time in the browser
4.Open another browser and access the city.aspx again by opening the application (ex: http://localhost:1034/YourApplicationName/City.aspx)
5.Now you select the Hyderabad then observe the changed time ( This means another version of the page is displaying)
6.Again you open another browser and copy paste the below URL (http://localhost:1034/YourApplicationName/City.aspx)
7. now observer the server time ( you will find the server time same as first browser). This means already one page is cached for Bangalore and now another page cached for Hyderabad.
8.Now you open another browser,access application and select Hyderabad.You will find the server time same as second browser time.
9.If you have any issues you can always contact .Net Training Bangalore
Note: Palle Technologies is the only institute who can provide excellent computer education at low cost and high quality. Get an assured placements by joining Palle Technologies.