我的思想来自孟子E章的例子,资料来源于http://dotnet.aspx.cc/article/b12283de-db20-4322-accc-12724442808a/read.aspx,我只不过将用DataGrid分页改成GridView分页罢了.在Visual Web Developer环境下运行通过.例子如下:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
btnFirst.Text = "最首页";
btnPrev.Text = "前一页";
btnNext.Text = "下一页";
btnLast.Text = "最后页";
GridView1.DataBind();
ShowPageChangedStatus();
}
private void ShowPageChangedStatus()
{
lblCurrentIndex.Text = "第" + (GridView1.PageIndex + 1).ToString() + "页";
lblPageCount.Text = "总共 " + GridView1.PageCount.ToString() + " 页";
}
public void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "next":
if (GridView1.PageIndex < (GridView1.PageCount - 1))
{
GridView1.PageIndex += 1;
}
break;
case "prev":
if (GridView1.PageIndex > 0)
{
GridView1.PageIndex -= 1;
}
break;
case "last":
GridView1.PageIndex = (GridView1.PageCount - 1);
break;
default:
GridView1.PageIndex = System.Convert.ToInt32(arg);
break;
}
GridView1.DataBind();
ShowPageChangedStatus();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int startIndex;
startIndex = GridView1.PageIndex * GridView1.PageSize;
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
ShowPageChangedStatus();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="pkey" PagerStyle-HorizontalAlign="Right" DataSourceID="SqlDataSource1" HorizontalAlign="Center" CellPadding="3" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging">
<Columns>
<asp:BoundField DataField="pkey" HeaderText="部门编号" InsertVisible="False" ReadOnly="True"
SortExpression="pkey">
<HeaderStyle BackColor="#C0C0FF" />
</asp:BoundField>
<asp:BoundField DataField="departmentName" HeaderText="部门名称" SortExpression="departmentName" >
<HeaderStyle BackColor="#C0C0FF" />
</asp:BoundField>
<asp:BoundField DataField="previousName" HeaderText="原先名称" SortExpression="previousName" >
<HeaderStyle BackColor="#C0C0FF" />
</asp:BoundField>
<asp:BoundField DataField="remark" HeaderText="附注" SortExpression="remark" >
<HeaderStyle BackColor="#C0C0FF" />
</asp:BoundField>
<asp:BoundField DataField="isSubCompany" HeaderText="子公司名" SortExpression="isSubCompany" >
<HeaderStyle BackColor="#C0C0FF" />
</asp:BoundField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" CancelText="取消" DeleteText="删除" EditText="编辑" SelectText="选择" UpdateText="更新" HeaderText="编辑" >
<HeaderStyle BackColor="#C0C0FF" />
</asp:CommandField>
</Columns>
<PagerStyle HorizontalAlign="Right"/>
<AlternatingRowStyle BackColor="#eeeeee" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:EHRConnectionString %>" DeleteCommand="DELETE FROM [DEPARTMENT] WHERE [pkey] = @original_pkey AND [departmentName] = @original_departmentName AND [previousName] = @original_previousName AND [remark] = @original_remark AND [isSubCompany] = @original_isSubCompany"
InsertCommand="INSERT INTO [DEPARTMENT] ([departmentName], [previousName], [remark], [isSubCompany]) VALUES (@departmentName, @previousName, @remark, @isSubCompany)"
OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [pkey], [departmentName], [previousName], [remark], [isSubCompany] FROM [DEPARTMENT]"
UpdateCommand="UPDATE [DEPARTMENT] SET [departmentName] = @departmentName, [previousName] = @previousName, [remark] = @remark, [isSubCompany] = @isSubCompany WHERE [pkey] = @original_pkey AND [departmentName] = @original_departmentName AND [previousName] = @original_previousName AND [remark] = @original_remark AND [isSubCompany] = @original_isSubCompany">
<DeleteParameters>
<asp:Parameter Name="original_pkey" Type="Int32" />
<asp:Parameter Name="original_departmentName" Type="String" />
<asp:Parameter Name="original_previousName" Type="String" />
<asp:Parameter Name="original_remark" Type="String" />
<asp:Parameter Name="original_isSubCompany" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="departmentName" Type="String" />
<asp:Parameter Name="previousName" Type="String" />
<asp:Parameter Name="remark" Type="String" />
<asp:Parameter Name="isSubCompany" Type="String" />
<asp:Parameter Name="original_pkey" Type="Int32" />
<asp:Parameter Name="original_departmentName" Type="String" />
<asp:Parameter Name="original_previousName" Type="String" />
<asp:Parameter Name="original_remark" Type="String" />
<asp:Parameter Name="original_isSubCompany" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="departmentName" Type="String" />
<asp:Parameter Name="previousName" Type="String" />
<asp:Parameter Name="remark" Type="String" />
<asp:Parameter Name="isSubCompany" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<div style="FONT-SIZE:9pt" align="center">
<asp:label id="lblPageCount" runat="server"></asp:label>
<asp:label id="lblCurrentIndex" runat="server"></asp:label>
<asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server"
Font-size="8pt" ForeColor="navy" CommandArgument="0"></asp:linkbutton>
<asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server"
Font-size="8pt" ForeColor="navy" CommandArgument="prev"></asp:linkbutton>
<asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server"
Font-size="8pt" ForeColor="navy" CommandArgument="next"></asp:linkbutton>
<asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server"
Font-size="8pt" ForeColor="navy" CommandArgument="last"></asp:linkbutton>
</div>
</div>
</form>
</body>
</html>