Version

Paging Template

Before You Begin

With the WebHierarchicalDataGrid™ control’s paging template feature, you can customize the look and feel of the pager to your specifications. You can add any control to the template; however, using the pager template requires that you handle the paging mechanism manually.

What You Will Accomplish

You will set up paging for the parent band of WebHierarchicalDataGrid using two buttons and a dropdown list. The two buttons handle moving to the previous and next pages while the dropdown list moves to the selected page.

Follow these Steps

  1. Bind WebHierarchicalDataGrid to a WebHierarchicalDataSource™ component so that it retrieves data from the Categories and products table. For more information on this see the Binding to Hierarchical Data Source topic.

  2. In the Microsoft® Visual Studio™ property window, locate the Bands property and click the ellipsis (…​) button to launch the Edit WebHierarchicalDataGrid Bands Dialog.

  3. For the parent band locate the Behaviors property and click the ellipsis (…​) button to launch the Behaviors Editor Dialog.

  4. Check the CheckBox next to Paging from the list on the left to add and enable the behavior. For this topic the PageSize is set to 3. The EnableInheritance property can be enabled to set Paging for the child bands.

  5. In the properties, expand PagingClientEvents and handle the PageIndexChanged event by entering an event handler name: WebHierarchicalDataGrid1_PageIndexChanged.

WebHierarchicalDataGrid Using Custom Paging Template 01.png
  1. Click Apply then Ok to close the editor.

  2. Right-click on WebHierarchicalDataGrid and highlight Edit Template. You will see an option for Pager Template.

Note
Note:

If you are using more than one behavior template, you will see Behavior Templates as a selection in the Edit Template submenu.

  1. Select Pager Template from the context menu. The WebHierarchicalDataGrid view changes to the template editing view.

Note
Note:

Depending on what behaviors you have added, you will see other templates in addition to the pager template.

  1. Add two HTML buttons and an ASP.NET DropDownList control to the template.

    1. Position the dropdown list between the two buttons.

    2. Set the value of the button before the dropdown list to Prev and set its id to PrevButton.

    3. Set the value of the second button to Next and set its id to NextButton.

    4. Double-click each button to add event handlers for them.

WebHierarchicalDataGrid Using Custom Paging Template 02.png
  1. Add a client-side handler named IndexChanged for the dropdown list. This will handle the manual paging of WebHierarchicalDataGrid when values are selected from the dropdown. The HTML for the dropdown list should look like the following:

In HTML:

<asp:DropDownList ID="DropDownList1" runat="server" onchange="return IndexChanged()">
</asp:DropDownList>
  1. Right-click WebHierarchicalDataGrid again and select End Template Editing. You are returned to the normal grid display. .

Populate the DropDownList control with the page numbers for your data in the WebHierarchicalDataGrid control’s_RowIslandDataBinding event.

  1. Obtain a reference to the dropdown list in your pager template. This requires accessing the PagerTemplateContainer—the container of controls inside the template—and finding the control inside.

  2. Populate the dropdown list with the number of pages. Handle the RowIslandDataBinding event and write the following code.

In Visual Basic:

AddHandler Me.WebHierarchicalDataGrid1.RowIslandDataBinding, AddressOf WebHierarchicalDataGrid1_RowIslandDataBinding
Protected Sub WebHierarchicalDataGrid1_RowIslandDataBinding(ByVal sender As Object, ByVal e As RowIslandEventArgs)
If e.RowIsland.DataMember = "SqlDataSource1_DefaultView" Then
   If Not IsPostBack Then
      Dim list1 As DropDownList =   DirectCast(e.RowIsland.Behaviors.Paging.PagerTemplateContainer.FindControl("DropDownList1"), DropDownList)
         'For simplicity, we are hard coding several list items. In a production application,
     'you would use logic to calculate how many page numbers should be added to the list.
         For i As Integer = 1 To 3
            list1.Items.Add(i.ToString())
         Next
      End If
   End If
End Sub

In C#:

this.WebHierarchicalDataGrid1.RowIslandDataBinding += new RowIslandEventHandler(WebHierarchicalDataGrid1_RowIslandDataBinding);
protected void WebHierarchicalDataGrid1_RowIslandDataBinding(object sender, RowIslandEventArgs e)
{
   if (e.RowIsland.DataMember == "SqlDataSource1_DefaultView")
   {
      if (!IsPostBack)
      {
         DropDownList list1 = (DropDownList)e.RowIsland.Behaviors.Paging.PagerTemplateContainer.FindControl("DropDownList1");
         //For simplicity, we are hard coding
         //several list items. In a production application,
         //you would use logic to calculate how many
         //page numbers should be added to the list.
         for (int i = 1; i $$<=$$ 3; i++)
         {
            list1.Items.Add(i.ToString());
         }
      }
   }
}
  1. Manually page WebHierarchicalDataGrid in the dropdown list’s IndexChanged event handler.

    1. Get a reference to WebHierarchicalDataGrid.

    2. Page WebHierarchicalDataGrid based on the value selected from the dropdown list.

Note
Note:

Notice the client ID of the DropDownList control. This is because the control is placed within several container controls (aka: the template). In this example, the client ID is hard-coded. In a production application, you would get this ID dynamically at runtime.

In Javascript:

function IndexChanged()
{
   var dropdownlist = document.getElementById("WebHierarchicalDataGrid1_ctl00_ctl01_DropDownList1");
   var grid = $find("WebHierarchicalDataGrid1");
   var parentGrid = grid.get_gridView();
   var newValue = dropdownlist.selectedIndex;
   parentGrid.get_behaviors().get_paging().set_pageIndex(newValue);
}
  1. Page WebHierarchicalDataGrid using the Prev button control’s client-side Click event handler.

    1. Get a reference to WebHierarchicalDataGrid.

    2. Get a reference to the dropdown list.

    3. Check that by decreasing the index, you do not exceed the bounds of the selectable pages.

    4. Go to the next page.

In Javascript:

var grid = $find("WebHierarchicalDataGrid1");
var parentGrid = grid.get_gridView();
var dropdownlist = document.getElementById("WebHierarchicalDataGrid1_ctl00_ctl01_DropDownList1");
if (parentGrid.get_behaviors().get_paging().get_pageIndex() > 0) {
   parentGrid.get_behaviors().get_paging().set_pageIndex(parentGrid.get_behaviors().get_paging().get_pageIndex() - 1);
}
  1. Do the same thing for the Next button event handler except that you are increasing the page index.

In Javascript:

var grid = $find("WebHierarchicalDataGrid1");
var parentGrid = grid.get_gridView();
var dropdownlist = document.getElementById("WebHierarchicalDataGrid1_ctl00_ctl01_DropDownList1");
if (parentGrid.get_behaviors().get_paging().get_pageIndex() < parentGrid.get_behaviors().get_paging().get_pageCount() - 1) {
   parentGrid.get_behaviors().get_paging().set_pageIndex(parentGrid.get_behaviors().get_paging().get_pageIndex() + 1);
}
  1. Add code to the body of the WebHierarchicalDataGrid1_PageIndexChanged client-side event you added earlier in step number 5.

In Javascript:

var grid = $find("WebHierarchicalDataGrid1");
var parentGrid = grid.get_gridView();
var dropdownlist = document.getElementById("WebHierarchicalDataGrid1_ctl00_ctl01_DropDownList1");
dropdownlist.options[parentGrid.get_behaviors().get_paging().get_pageIndex()].selected = true;
  1. Run the application. The parent band of WebHierarchicalDataGrid’s pager consists of two buttons and a dropdown list, each of which can be used for paging.

WebHierarchicalDataGrid Using Custom Paging Template 03.png