Custom GridView Pager

        <asp:GridView ID="TheGridView" runat="server" AllowPaging="True" PageSize="1">
            <PagerSettings Position="Top" />
            <PagerTemplate>
                <asp:LinkButton CommandName="Page" CommandArgument="First" ID="FirstLinkButton" runat="server">╟</asp:LinkButton>
                <asp:LinkButton CommandName="Page" CommandArgument="Prev" ID="PrevLinkButton" runat="server">├</asp:LinkButton>
                [Page
                <asp:DropDownList ID="PageDropDownList" runat="server" OnPreRender="PageDropDownList_PreRender" AutoPostBack="True" OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged">
                </asp:DropDownList>
                of
                <%= TheGridView.PageCount%>]
                <asp:LinkButton CommandName="Page" CommandArgument="Next" ID="NextLinkButton" runat="server">┤</asp:LinkButton>
                <asp:LinkButton CommandName="Page" CommandArgument="Last" ID="LastLinkButton" runat="server">╢</asp:LinkButton>
                 Records per page: <asp:TextBox ID="PageSizeTextBox" Style="font-family: 'Courier New', Courier, monospace; color: #000000; text-align: center; width: 3em; vertical-align: middle;" runat="server" ToolTip="Type the number of records you want to see at one time, then press Enter." OnPreRender="PageSizeTextBox_PreRender" OnTextChanged="PageSizeTextBox_TextChanged" MaxLength="3"></asp:TextBox>
                 <asp:RangeValidator ID="PageSizeRangeValidator" runat="server" BackColor="Yellow" ControlToValidate="PageSizeTextBox" ErrorMessage="Must be a number from 1 to 999." ForeColor="Red" MaximumValue="999" MinimumValue="1" SetFocusOnError="True" Display="Dynamic" Type="Integer"></asp:RangeValidator>
            </PagerTemplate>
        </asp:GridView>

protected void PageDropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
{
 DropDownList PageDropDownList = (DropDownList)sender;
 GridView ThisGridView = (GridView)PageDropDownList.NamingContainer.NamingContainer;
 ThisGridView.PageIndex = PageDropDownList.SelectedIndex;
 ThisGridView.DataBind();
}

protected void PageDropDownList_PreRender(object sender, System.EventArgs e)
{
 DropDownList PageDropDownList = (DropDownList)sender;
 PageDropDownList.Items.Clear();
 GridView ThisGridView = (GridView)PageDropDownList.NamingContainer.NamingContainer;
 for (int I = 1; I <= ThisGridView.PageCount; I++) {
  ListItem PageListItem = new ListItem();
  PageListItem.Text = I.ToString;
  PageListItem.Value = (I - 1).ToString;
  if (ThisGridView.PageIndex + 1 == I) {
   PageListItem.Selected = true;
  }
  PageDropDownList.Items.Add(PageListItem);
 }
}

protected void PageSizeTextBox_PreRender(object sender, System.EventArgs e)
{
 TextBox ThisTextBox = (TextBox)sender;
 GridView ThisGridView = (GridView)ThisTextBox.NamingContainer.NamingContainer;
 ThisTextBox.Text = ThisGridView.PageSize.ToString;
}

protected void PageSizeTextBox_TextChanged(object sender, System.EventArgs e)
{
 TextBox ThisTextBox = (TextBox)sender;
 GridView ThisGridView = (GridView)ThisTextBox.NamingContainer.NamingContainer;
 ThisGridView.PageIndex = 1;
 ThisGridView.PageSize = Convert.ToInt32(ThisTextBox.Text);
 ThisGridView.TopPagerRow.Visible = true;
}

No comments:

Post a Comment