用存储过程实现分页,除了上一页,下一页,第一页,和末页外还要有go按钮,以及go到那里的文本框。
前台:
<asp:GridView ID="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound">
</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="Label1" runat="server"></asp:Label>
<asp:TextBox ID="txtPageNumber" runat="server" Height="26px" Width="43px"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="txtPageNumber" ErrorMessage="必须为整数!" ForeColor="#FF3300"
Operator="DataTypeCheck" Type="Integer" Display="Dynamic"></asp:CompareValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="txtPageNumber" ErrorMessage="输入数据不合法!" ForeColor="Red"
MaximumValue='9' MinimumValue="1" Display="Dynamic"></asp:RangeValidator>
<asp:Button ID="btnGo" runat="server" Text="Go" onclick="btnGo_Click" />
<br />
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:HiddenField ID="HiddenField2" runat="server" />
后台:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
bindStudent(1);
}
private void bindStudent(int pageIndex)
{
string constr = ConfigurationManager.ConnectionStrings["studentConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_Student_Select_by_Page_rowNumber";
cmd.Parameters.AddWithValue("@pageSize", 3);
cmd.Parameters.Add("@pageCount", System.Data.DbType.Int32).Direction = ParameterDirection.Output;
cmd.Parameters.AddWithValue("@pageIndex", pageIndex);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
int pageCount = Convert.ToInt32(cmd.Parameters["@pageCount"].Value);
this.HiddenField1.Value = pageCount.ToString();
this.HiddenField2.Value = pageIndex.ToString();
}
}
}
protected void btnFirst_Click(object sender, EventArgs e)
{
this.bindStudent(1);
}
protected void btnPrev_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(this.HiddenField2.Value);
if (index > 1)
{
index--;
this.bindStudent(index);
}
}
protected void btnLast_Click(object sender, EventArgs e)
{
this.bindStudent(Convert.ToInt32(this.HiddenField1.Value));
}
protected void btnNext_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(this.HiddenField2.Value);
int pageCount = Convert.ToInt32(this.HiddenField1.Value);
if (index<pageCount)
{
index++;
this.bindStudent(index);
}
}
protected void btnGo_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(txtPageNumber.Text) <= Convert.ToInt32(HiddenField1.Value))
{
this.bindStudent(Convert.ToInt32(txtPageNumber.Text));
}
else
{
Response.Write("您输入的页数超出了总页数,如有需要请重新输入!");
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Label1.Text = "第" + (HiddenField2.Value).ToString() + "页,共" + HiddenField1.Value.ToString() + "页";
}
----------------------------------------------------