<asp:GridView ID="GridView2" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="sid"
onrowdatabound="GridView2_RowDataBound" PageSize="3"
onpageindexchanging="GridView1_PageIndexChanging" ShowFooter="True">
<Columns>
<asp:BoundField DataField="sid" HeaderText="sid" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:BoundField DataField="sname" HeaderText="sname" SortExpression="sname" />
<asp:BoundField DataField="classid" HeaderText="classid"
SortExpression="classid" />
<asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
<asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
<asp:CheckBoxField DataField="isking" HeaderText="isking"
SortExpression="isking" />
<asp:BoundField DataField="photo" HeaderText="photo" SortExpression="photo" />
</Columns>
<PagerSettings FirstPageText="|&lt;" LastPageText="&gt;|"
Mode="NextPreviousFirstLast" Visible="False" />
<PagerTemplate>
<asp:Button ID="Button1" runat="server" CommandArgument="First"
CommandName="page" Text="|<" />
<asp:Button ID="Button2" runat="server" CommandArgument="prev"
CommandName="page" Text="<" />
<asp:Button ID="Button3" runat="server" CommandArgument="next"
CommandName="page" Text=">" />
<asp:Button ID="Button4" runat="server" CommandArgument="Last"
CommandName="page" Text=">|" />
<asp:Label ID="Label1" runat="server"></asp:Label>
</PagerTemplate>
</asp:GridView>
<br />
<br />
<asp:Button ID="btnFirst" runat="server" onclick="btnFirst_Click" Text="第一页" />
<asp:Button ID="btnPrev" runat="server" onclick="btnPrev_Click" Text="上一页" />
<asp:Button ID="btnNext" runat="server" onclick="btnNext_Click" Text="下一页" />
<asp:Button ID="btnLast" runat="server" onclick="btnLast_Click" Text="末页" />
<asp:Label ID="Label2" runat="server"></asp:Label>
后台:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindStudent();
}
}
private void BindStudent()
{
string sql = "select * from student";
DataTable dt = SQLHelper.ExecuteDataTable(sql);
this.GridView2.DataSource = dt;
this.GridView2.DataBind();
}
protected void btnFirst_Click(object sender, EventArgs e)
{
this.GridView2.PageIndex = 0;
this.BindStudent();
}
protected void btnLast_Click(object sender, EventArgs e)
{
this.GridView2.PageIndex = this.GridView2.PageCount - 1;
this.BindStudent();
}
protected void btnPrev_Click(object sender, EventArgs e)
{
int index = this.GridView2.PageIndex;
if (index >= 1)
{
index--;
}
this.GridView2.PageIndex = index;
this.BindStudent();
}
protected void btnNext_Click(object sender, EventArgs e)
{
int index = this.GridView2.PageIndex;
if (index < this.GridView2.PageCount + 1)
{
index++;
}
this.GridView2.PageIndex = index;
this.BindStudent();
}
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
Label2.Text = "第" + (GridView2.PageIndex + 1).ToString() + "页,共" + GridView2.PageCount + "页";
}
-------------------------------------------------------------------------------------------------
Web.config:
<connectionStrings>
<add name="studentConnectionString" connectionString="Data Source=PC_THINK-THINK;Initial Catalog=student;Persist Security Info=True;User ID=sa;Password=111111"
providerName="System.Data.SqlClient" />
<add name="studentConnectionString2" connectionString="Data Source=PC_THINK-THINK;Initial Catalog=student;Persist Security Info=True;User ID=sa;Password=111111"
providerName="System.Data.SqlClient" />
</connectionStrings>
SQLHelper.cs:
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
{
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(sql,connStr);
if (pms != null)
{
adapter.SelectCommand.Parameters.AddRange(pms);
}
adapter.Fill(dt);
return dt;
}
在这道题中用到了数据集 DataSet1.xsd
----------------------------------------------------------------------------------------------------