演练:使用 ListView Web 服务器控件对数据进行显示、分页和排序
DataPager 控件 注意DataPager控件只能和ListView控件使用,不能和其他数据绑定控件使用!
以下ListView 里面的代码都是按照上面链接里面说的步骤执行生成的代码…,不过下面的删除功能有问题,因为删除的时候他涉及到外键关系…
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="datasUseGridView.aspx.cs" Inherits="testWeb.databing.datasUseGridView" EnableViewState="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>ListView和DataPager</title> </head> <body> <form id="form1" runat="server"> <div> <asp:ListView ID="ListView1" runat="server" DataKeyNames="DepartmentID" DataSourceID="SqlDataSource2" InsertItemPosition="LastItem"> <AlternatingItemTemplate> <tr style="background-color: #FFFFFF; color: #284775;"> <td> <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删?除y" OnClientClick="return confirm('Are you sure?');" /> <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编à辑-" /> </td> <td> <asp:Label ID="DepartmentIDLabel" runat="server" Text='<%# Eval("DepartmentID") %>' /> </td> <td> <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' /> </td> <td> <asp:Label ID="GroupNameLabel" runat="server" Text='<%# Eval("GroupName") %>' /> </td> </tr> </AlternatingItemTemplate> <EditItemTemplate> <tr style="background-color: #999999;"> <td> <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更ü新?" /> <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取?消?" /> </td> <td> <asp:Label ID="DepartmentIDLabel1" runat="server" Text='<%# Eval("DepartmentID") %>' /> </td> <td> <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' /> </td> <td> <asp:TextBox ID="GroupNameTextBox" runat="server" Text='<%# Bind("GroupName") %>' /> </td> </tr> </EditItemTemplate> <EmptyDataTemplate> <table runat="server" style="background-color: #FFFFFF; border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px;"> <tr> <td> 未′返μ回?数y据Y。£ </td> </tr> </table> </EmptyDataTemplate> <InsertItemTemplate> <tr style=""> <td> <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插?入?" /> <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清?除y" /> </td> <td> </td> <td> <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' /> </td> <td> <asp:TextBox ID="GroupNameTextBox" runat="server" Text='<%# Bind("GroupName") %>' /> </td> </tr> </InsertItemTemplate> <ItemTemplate> <tr style="background-color: #E0FFFF; color: #333333;"> <td> <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删?除y" /> <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编à辑-" /> </td> <td> <asp:Label ID="DepartmentIDLabel" runat="server" Text='<%# Eval("DepartmentID") %>' /> </td> <td> <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' /> </td> <td> <asp:Label ID="GroupNameLabel" runat="server" Text='<%# Eval("GroupName") %>' /> </td> </tr> </ItemTemplate> <LayoutTemplate> <table runat="server"> <tr runat="server"> <td runat="server"> <table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF; border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px; font-family: Verdana, Arial, Helvetica, sans-serif;"> <tr runat="server" style="background-color: #E0FFFF; color: #333333;"> <th runat="server"> </th> <th runat="server"> DepartmentID </th> <th runat="server"> Name </th> <th runat="server"> GroupName </th> </tr> <tr id="itemPlaceholder" runat="server"> </tr> </table> </td> </tr> <tr runat="server"> <td runat="server" style="text-align: center; background-color: #5D7B9D; font-family: Verdana, Arial, Helvetica, sans-serif; color: #FFFFFF"> </td> </tr> </table> </LayoutTemplate> <SelectedItemTemplate> <tr style="background-color: #E2DED6; font-weight: bold; color: #333333;"> <td> <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删?除y" /> <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编à辑-" /> </td> <td> <asp:Label ID="DepartmentIDLabel" runat="server" Text='<%# Eval("DepartmentID") %>' /> </td> <td> <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' /> </td> <td> <asp:Label ID="GroupNameLabel" runat="server" Text='<%# Eval("GroupName") %>' /> </td> </tr> </SelectedItemTemplate> </asp:ListView>//数据源控件 <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>" DeleteCommand="DELETE FROM HumanResources.Department WHERE (DepartmentID = @DepartmentID) " InsertCommand="INSERT INTO HumanResources.Department(Name, GroupName) VALUES (@Name, @GroupName) " SelectCommand="SELECT DepartmentID, Name, GroupName FROM HumanResources.Department " UpdateCommand="UPDATE HumanResources.Department SET Name = @Name, GroupName = @GroupName WHERE (DepartmentID = @DepartmentID) "> <DeleteParameters> <asp:Parameter Name="DepartmentID" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="Name" /> <asp:Parameter Name="GroupName" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="Name" /> <asp:Parameter Name="GroupName" /> <asp:Parameter Name="DepartmentID" /> </UpdateParameters> </asp:SqlDataSource><%--默认的--%> <%--<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="5"> <Fields> <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" /> <asp:NumericPagerField /> <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" /> </Fields> </asp:DataPager>--%> <%--自定义分页控件样式--%> <asp:DataPager runat="server" ID="DataPager2" PageSize="8" PagedControlID="ListView1"> <Fields> <asp:TemplatePagerField> <PagerTemplate> <asp:TextBox ID="CurrentRowTextBox" runat="server" AutoPostBack="true" Text="<%# Container.StartRowIndex + 1%>" Columns="1" Style="text-align: right" OnTextChanged="CurrentRowTextBox_OnTextChanged" /> to <asp:Label ID="PageSizeLabel" runat="server" Font-Bold="true" Text="<%# Container.StartRowIndex + Container.PageSize > Container.TotalRowCount ? Container.TotalRowCount : Container.StartRowIndex + Container.PageSize %>" /> of <asp:Label ID="TotalRowsLabel" runat="server" Font-Bold="true" Text="<%# Container.TotalRowCount %>" /> </PagerTemplate> </asp:TemplatePagerField> <asp:NextPreviousPagerField ShowFirstPageButton="true" ShowLastPageButton="true" FirstPageText="|<< " LastPageText=" >>|" NextPageText=" > " PreviousPageText=" < " /> </Fields> </asp:DataPager> </div> </form> </body> </html> 后台…using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace testWeb.databing { public partial class datasUseGridView : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void CurrentRowTextBox_OnTextChanged(object sender, EventArgs e) { TextBox t = (TextBox)sender; DataPager pager = (DataPager)this.FindControl("DataPager2"); //如?果?DataPager放?在úListView控?件t里?面?,则ò需è要a使1用?下?面?的?方?式?获?取? //DataPager pager = (DataPager)ListView1.FindControl("DataPager2"); pager.SetPageProperties(Convert.ToInt32(t.Text) - 1, pager.PageSize, true); } } }