Wednesday, September 23, 2015

Caching multiple responses for a single webform

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)  
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 DropDownList1 control.


If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

Related Posts:

  • Creating your first ASP.NET website In this video session 1. We will learn about using visual studio 2. Creating your first ASP.NET web application 3. Learn about different windows in … Read More
  • C# ADO.NET SqlCommand - ExecuteScalar The ExecuteScalar() in C# SqlCommand Object is using for retrieve a single value from Database after the execution of the SQL St… Read More
  • What is ASP.NET ASP.NET is a Web application framework developed by Microsoft to build dynamic data driven Web applications and Web services.  1. ASP.NET is a … Read More
  • What is viewstate In this video session, we will learn about 1. Stateless nature of HTTP protocol 2. How a webform is processed 3. What is ViewState 4. The role of Vi… Read More
  • OLEDB Connection string The C# OleDbConnection instance takes Connection String as argument and pass the value to the Constructor statement. An instanc… Read More

1 comment:

  1. Thanks for your blog....
    Hope you are looking to start perfect career in ETHICAL HACKING. We have excellent training institute in Hyderabad with real time experts. For further information please visit our site.
    CEH Training In Hyderabad


    ReplyDelete