This tutorial will give an introduction to using the PagerTemplate of a GridView control. The PagerTemplate allows us to set a certain layout that the pager of a GridView will conform to. In this example, we will create the PagerTemplate too show the current page and number of pages, as well as a dropdown menu to navigate between pages.
First, we will add the Connection String to the sample database, NorthWind:
First, we will add the Connection String to the sample database, NorthWind:
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NORTHWND.MDF;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />
</connectionStrings> |
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
Next, we add the GridView to our ASPX page.Note the code between the <PagerTemplate> tags, this is the layout for the GridView pager:
<form id="form1" runat="server">
<asp:GridView id="CustomersGridView"
</form>
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="False" allowpaging="True" ondatabound="CustomersGridView_DataBound" runat="server" DataKeyNames="CustomerID"> <PagerStyle forecolor="Blue" backcolor="LightBlue"/> <PagerTemplate>
<table width="100%">
<tr>
</table>
<td style="width:70%">
<asp:label id="MessageLabel"
forecolor="Blue"
text="Select a page:" runat="server"/> <asp:dropdownlist id="PageDropDownList"
autopostback="true"
onselectedindexchanged="PageDropDownList_SelectedIndexChanged" runat="server"/> </td> <td style="width:70%; text-align:right">
<asp:label id="CurrentPageLabel" forecolor="Blue" runat="server"/>
</td></tr> </PagerTemplate>
<Columns>
</asp:GridView>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
</Columns><asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" /> <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" /> <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" /> <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" /> <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" /> <asp:SqlDataSource id="CustomersSqlDataSource"
selectcommand="SELECT [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] FROM [Customers]"
</asp:SqlDataSource>connectionstring="<%$ ConnectionStrings:ConnectionString %>" runat="server">
|
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
The code behind will look something like this: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
}{ } protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e) {
GridViewRow pagerRow = CustomersGridView.BottomPagerRow;
}DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList"); CustomersGridView.PageIndex = pageList.SelectedIndex; protected void CustomersGridView_DataBound(Object sender, EventArgs e) {
GridViewRow pagerRow = CustomersGridView.BottomPagerRow;
}DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList"); Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel"); if (pageList != null) {
for (int i = 0; i < CustomersGridView.PageCount; i++)
}{
int pageNumber = i + 1;
}ListItem item = new ListItem(pageNumber.ToString()); if (i == CustomersGridView.PageIndex) {
item.Selected = true;
}pageList.Items.Add(item); if (pageLabel != null) {
int currentPage = CustomersGridView.PageIndex + 1;
}pageLabel.Text = "Page " + currentPage.ToString() +
" of " + CustomersGridView.PageCount.ToString();
|
source http://www.aspnettutorials.com/tutorials/controls/gvpagertemplate-csharp.aspx
No comments:
Post a Comment