Intersoft WebGrid Documentation
Walkthrough: Using cookie to save FilterRow values
See Also Send comments on this topic.

Glossary Item Box

Some developers want to create a customized feature for each user. For example they want to customize the filter area of WebGrid, since some users prefer to for example directly show the list of customers' name starting with the word "A". So Every time the page is first loaded, it will check whether a cookie for that page exist. If the cookie exists, then the cookie value will be captured and a filter will be added to a column based on the cookie value

During this walkthrough, you will learn how to do the following:

 Prerequisites

In order to complete this walkthrough, you will need the following:

  • Access to the Microsoft Access Northwind database
  • Visual Studio 2005 Application.

 Step-By-Step Instructions

To Use Cookie to Save Filter Row values

  1. Launch Visual Studio.NET 2005.
  2. Click on File menu, then select New and click Web Site.
  3. Select ASP.NET Web Site in the Template box and set Location to HTTP.
  4. Named the Web Site and click OK.
  5. Right-click on Project's name and select Add New Item.
  6. Select Intersoft AppForm in the My Templates box and named it as Walkthrough.aspx.
  7. Drag WebGrid instance from ToolBar to WebForm.
  8. In the Solution Explorer, right-click on App_Data and select Add Existing Item.
  9. Browse and add NorthWind.mdb in C:\Program Files\Intersoft Solutions\Data (Default installation folder).
  10. Click the SmartTag on the upper right of the WebGrid.
  11. In ChooseDataSource field, choose <New data source...>.



  12. In DataSourceConfigurationWizard, choose AccessDatabase and click OK.



  13. Browse and select the database in App_Data (NorthWind.mdb) and click Next.



  14. Select the table that you want to use and click Next.



  15. In this step, you can view the table's database using Test Query button. Otherwise, click Finish to proceed to the next step.
  16. Finally, choose Retrieve Structure action in Connected to Data Source Control Wizard and click OK to apply all settings.


  17. Set the AllowFilter Property to Yes.
  18. Create a HTML button with it's onClick attribute set to the name of your JavaScript function for example
    <INPUT type="button" value="SaveFilterCookie" onclick="SaveFilterCookie();">
  19. Insert this javaScript function on the head area of your HTML .

    JavaScript Copy Code
    <script language="javascript" type="text/javascript">
     function SaveFilterCookie()
     {
      var webGrid = ISGetObject("WebGrid1");
      var rowFilter = webGrid.Tables["Customers"].FilteredColumns;
      var CustomerIDFilter;
      if(rowFilter.GetNamedItem("CustomerID") != null)
      {
       CustomerIDFilter = rowFilter.GetNamedItem("CustomerID").FilterText;
      }
      else
      {
       CustomerIDFilter = "";
      }    
      webGrid.AddInput("CustomerIDFilter", CustomerIDFilter);
      webGrid.SendCustomRequest();
     }
    </script>
    

  20. On the InitializePostBack event handler of the grid, you write code to save cookie on the fly.

    C# Copy Code
    private void WebGrid1_InitializePostBack(object sender, ISNet.WebUI.WebGrid.PostbackEventArgs e)
    {
     if (e.Action == PostBackAction.Custom)
     {
      //request the CustomerIDFilter and store it in a cookie called UserPreference
      String CustomerIDFilter = Request["CustomerIDFilter"];
      HttpCookie myCookie = new HttpCookie("UserPreference");
      myCookie["CustomerIDFilter"] = CustomerIDFilter;
      myCookie.Expires = DateTime.Now.AddDays(2);
      Response.Cookies.Add(myCookie);
     }
    }
    

  21. On the PageLoad event handler of the Page, write codes to check whether the cookie exist or not. If it does exist then the cookie value will be taken and used as a filter.

    C# Copy Code
    private void Page_Load(object sender, System.EventArgs e)
    {
     if(!IsPostBack)
     {
      if (Request.Cookies["UserPreference"] != null)
      {
       string columnMember = "CustomerID";
       string filterText = Request.Cookies["UserPreference"]["CustomerIDFilter"];
       ISNet.WebUI.WebGrid.WebGridFilter filter = new ISNet.WebUI.WebGrid.WebGridFilter(columnMember,ISNet.WebUI.WebGrid.ColumnFilterType.Like,filterText);
       this.WebGrid1.RootTable.FilteredColumns.Add(filter);
      }
     } 
    }
    

See Also

©2012 Intersoft Solutions Corp. All Rights Reserved.