Saturday 26 August 2017

Caching multiple responses for a single webform

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)  
as  
Begin  
 if(@ProductName = 'All')  
  Begin  
   Select Id, Name, Description  
   from tblProducts  
  End  
 Else  
  Begin  
   Select Id, Name, Description  
   from tblProducts  
   where Name = @ProductName  
  End  
End 

WebForm1.aspx HTML:
<div style="font-family:Arial">
    Select Product: 
    <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        <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>
    </asp:DropDownList>
    <br />
    <br />
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    <br />
    <br />
    Server Time: 
    <asp:Label ID="Label1" runat="server"></asp:Label>
    <br />
    <br />
    Client Time:
    <script type="text/javascript">
        document.write(Date());
    </script>
</div>

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

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    GetProductByName(DropDownList1.SelectedValue);
}

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;
    da.SelectCommand.Parameters.Add(paramProductName);

    DataSet DS = new DataSet();
    da.Fill(DS);
    GridView1.DataSource = DS;
    GridView1.DataBind();
}

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 DropDownList1control.

No comments:

Post a Comment

Recent Post

Parallel Task in .Net 4.0