Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Download

Caching multiple responses for a single webform - Part 120

Suggested Videos
Part 117 - Assigning an image to the composite custom control in visual studio tool box
Part 118 - Difference between user controls and custom controls
Part 119 - Caching in ASP.NET

In this video we will discuss about, caching multiple responses for a single webform. Please watch Part -119 from ASP.NET video tutorial, before proceeding with this video.

First create a stored procedure to get products from "tblProducts" table by product name. This procedure returns "ALL Products", if "All" is supplied as the product name, else, only the product whose name matches with the parameter "@ProductName" is returned.
Create Procedure spGetProductByName  
@ProductName nvarchar(50)  
 if(@ProductName = 'All')  
   Select Id, Name, Description  
   from tblProducts  
   Select Id, Name, Description  
   from tblProducts  
   where Name = @ProductName  

WebForm1.aspx HTML:
<div style="font-family:Arial">
    Select Product: 
    <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" 
        <asp:ListItem Text="All" Value="All"></asp:ListItem>
        <asp:ListItem Text="Laptops" Value="Laptops"></asp:ListItem>
        <asp:ListItem Text="iPhone" Value="iPhone"></asp:ListItem>
        <asp:ListItem Text="LCD TV" Value="LCD TV"></asp:ListItem>
        <asp:ListItem Text="Desktop" Value="Desktop"></asp:ListItem>
    <br />
    <br />
    <asp:GridView ID="GridView1" runat="server">
    <br />
    <br />
    Server Time: 
    <asp:Label ID="Label1" runat="server"></asp:Label>
    <br />
    <br />
    Client Time:
    <script type="text/javascript">

WebForm1.aspx.cs Code:
protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
    Label1.Text = DateTime.Now.ToString();

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

private void GetProductByName(string ProductName)
    string CS = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(CS);
    SqlDataAdapter da = new SqlDataAdapter("spGetProductByName", con);
    da.SelectCommand.CommandType = CommandType.StoredProcedure;

    SqlParameter paramProductName = new SqlParameter();
    paramProductName.ParameterName = "@ProductName";
    paramProductName.Value = ProductName;

    DataSet DS = new DataSet();
    GridView1.DataSource = DS;

Include "@OutputCache" attribute with the followig settings. Notice that, since "VaryByParam" is set to "None", only one response of "WebForm1" is cached.
<%@ OutputCache Duration="60" VaryByParam="None"%>

Now change the "@OutputCache" attribute as shown below.
<%@ OutputCache Duration="60" VaryByParam="DropDownList1"%>

Since "VaryByParam" is set to "DropDownList1", up to 5 different responses will be cached for this Web form. One for each possible selection from DropDownList1 control.


  1. Respected Sir,

    Please tell me why caching of the web form hasn't happened at the page load when VaryByParam=None.......Why ASP.Net engine allowed drop-down change result to be displayed first time and then cached result for subsequent request till the cache time expires......When i loaded the form it should have cached at that time why it cached after first dropdown change event.......Is there any reason for this....Please tell me....

    1. Hello Sir....After google search i got one fine article which says....

      Leaving the VaryByParam attribute set to 'none', as shown in Listing 9-1, means that one copy of the page will be cached for each request type (GET, HEAD, or POST). Subsequent requests of the same type will be served a cached response for that type of request. Table 9-1 shows the complete set of attributes available with the OutputCache directive.

      I believe this is the answer of the question.......So when the load is done first time it is a get request and it is cached....When dropdown changes it is post request and it is seperately cached.....After that all post request will be served from cached page being build because of Post request type and all get request will be served from cached page being build because of get request type.....Thus depending upon the request type there can be multiple cache pages even if VaryByParam=None..........


It would be great if you can help share these free resources