我在我的项目中实现了pagerTemplate功能,并参考了Here。但问题是下拉列表不能正常工作。当我更改选择时,gridview不会返回确切的结果。请按照我在此处实施的方式查看代码。
<asp:GridView ID="grdTeacherProfile" runat="server" Width="100%" border="1" Style="border: 1px solid #E5E5E5;" CellPadding="3" FooterStyle-BackColor="#e3e3e3"
AutoGenerateColumns="false" AllowPaging="true" CssClass="hoverTable" DataKeyNames="Id" PageSize="3" ShowFooter="true" HeaderStyle-CssClass="k-grid td"
OnDataBound="grdTeacherProfile_DataBound" OnPageIndexChanging="grdTeacherProfile_PageIndexChanging" OnRowDeleting="grdTeacherProfile_RowDeleting"
OnRowUpdating="grdTeacherProfile_RowUpdating" OnRowEditing="grdTeacherProfile_RowEditing" OnRowCancelingEdit="grdTeacherProfile_RowCancelingEdit"
EmptyDataText="No records found">
<AlternatingRowStyle BackColor="#E9E9E9" />
<Columns>
<asp:BoundField DataField="first_name" HeaderText="First Name" ItemStyle-Width="25" ControlStyle-CssClass="k-grid td" />
<asp:BoundField DataField="last_name" HeaderText="Last Name" ItemStyle-Width="30" ControlStyle-CssClass="k-grid td" />
<asp:BoundField DataField="dob" HeaderText="Date of Birth" ItemStyle-Width="20" ControlStyle-CssClass="k-grid td" ApplyFormatInEditMode="true" DataFormatString="{0:d}" />
<asp:BoundField DataField="gender" HeaderText="Gender" ItemStyle-Width="20" ControlStyle-CssClass="k-grid td" />
<asp:BoundField DataField="designation" HeaderText="Designation" ItemStyle-Width="20" ControlStyle-CssClass="k-grid td" />
<asp:BoundField DataField="joining_date" HeaderText="Joining Date" ItemStyle-Width="20" ControlStyle-CssClass="k-grid td" ApplyFormatInEditMode="true" DataFormatString="{0:d}" />
<asp:BoundField DataField="leaving_date" HeaderText="Leaving Date" ItemStyle-Width="20" ControlStyle-CssClass="k-grid td" ApplyFormatInEditMode="true" DataFormatString="{0:d}" />
<asp:BoundField DataField="active" HeaderText="Active" ItemStyle-Width="25" ControlStyle-CssClass="k-grid td" />
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="5%">
<ItemTemplate>
<asp:ImageButton ID="btnDelete" AlternateText="Delete" ImageUrl="~/images/delete.png" runat="server" Width="15" Height="15" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
<asp:ImageButton ID="btnEdit" ImageUrl="~/images/edit.png" ControlStyle-Width="15" CausesValidation="false" ControlStyle-Height="15" runat="server"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#D8DADA" Font-Bold="True" />
<RowStyle BackColor="white" BorderStyle="Solid" BorderColor="#a8a8a8" BorderWidth="1px" Height="35" />
<PagerTemplate>
<table style="width:100%">
<tr>
<td style="width:auto; float:right;">
<asp:Label ID="MessageLabel" runat="server" Text="Select the page"></asp:Label>
<asp:DropDownList ID="ddlPageSort" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlPageSort_SelectedIndexChanged" ></asp:DropDownList>
</td>
<td style="width:auto; float:left;">
<asp:Label ID="CurrentPageLabel" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
另见背后的代码: -
protected void grdTeacherProfile_DataBound(object sender, EventArgs e)
{
GridViewRow pagerRow = grdTeacherProfile.BottomPagerRow;
// Retrieve the DropDownList and Label controls from the row.
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("ddlPageSort");
Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
if (pageList != null)
{
for (int i = 0; i < grdTeacherProfile.PageCount; i++)
{
// Create a ListItem object to represent a page.
int pageNumber = i + 1;
ListItem item = new ListItem(pageNumber.ToString());
if (i == grdTeacherProfile.PageIndex)
{
item.Selected = true;
}
// Add the ListItem object to the Items collection of the
// DropDownList.
pageList.Items.Add(item);
}
}
if (pageLabel != null)
{
// Calculate the current page number.
int currentPage = grdTeacherProfile.PageIndex + 1;
// Update the Label control with the current page information.
pageLabel.Text = "Page " + currentPage.ToString() + " of " + grdTeacherProfile.PageCount.ToString();
}
}
protected void ddlPageSort_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow pagerRow = grdTeacherProfile.BottomPagerRow;
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("ddlPageSort");
grdTeacherProfile.PageIndex = pageList.SelectedIndex;
}