Tips and tricks for .NET using ASP and VB code.

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">&#9567;</asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Prev" ID="PrevLinkButton" runat="server">&#9500;</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">&#9508;</asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Last" ID="LastLinkButton" runat="server">&#9570;</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 Sub PageDropDownList_SelectedIndexChanged(sender As Object, e As System.EventArgs)
Dim PageDropDownList As DropDownList = CType(sender, DropDownList)
Dim ThisGridView As GridView = CType(PageDropDownList.NamingContainer.NamingContainer, GridView)
ThisGridView.PageIndex = PageDropDownList.SelectedIndex
ThisGridView.DataBind()
End Sub

Protected Sub PageDropDownList_PreRender(sender As Object, e As System.EventArgs)
Dim PageDropDownList As DropDownList = CType(sender, DropDownList)
PageDropDownList.Items.Clear()
Dim ThisGridView As GridView = CType(PageDropDownList.NamingContainer.NamingContainer, GridView)
For I As Integer = 1 To ThisGridView.PageCount
Dim PageListItem As New ListItem
PageListItem.Text = I.ToString
PageListItem.Value = (I - 1).ToString
If ThisGridView.PageIndex + 1 = I Then
PageListItem.Selected = True
End If
PageDropDownList.Items.Add(PageListItem)
Next
End Sub

Protected Sub PageSizeTextBox_PreRender(sender As Object, e As System.EventArgs)
Dim ThisTextBox As TextBox = CType(sender, TextBox)
Dim ThisGridView As GridView = CType(ThisTextBox.NamingContainer.NamingContainer, GridView)
ThisTextBox.Text = ThisGridView.PageSize.ToString
End Sub

Protected Sub PageSizeTextBox_TextChanged(sender As Object, e As System.EventArgs)
Dim ThisTextBox As TextBox = CType(sender, TextBox)
Dim ThisGridView As GridView = CType(ThisTextBox.NamingContainer.NamingContainer, GridView)
ThisGridView.PageIndex = 1
ThisGridView.PageSize = CInt(ThisTextBox.Text)
ThisGridView.TopPagerRow.Visible = True
End Sub

No comments:

Post a Comment

Search This Blog