1 用存储过程实现分页,除了上一页,下一页,第一页,和末页外还要有go按钮,以及go到那里的文本框。另外还要在Lable显示“当前x页,一共y页”。注意验证控件的使用和 链接存储过程的内容。
前台代码:
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="id" HeaderText="行号" />
<asp:BoundField DataField="sid" HeaderText="编号" />
<asp:BoundField DataField="sname" HeaderText="姓名" />
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:BoundField DataField="age" HeaderText="年龄" />
</Columns>
</asp:GridView>
<asp:Button ID="btnFirst" runat="server" Text="|<"
οnclick="btnFirst_Click" />
<asp:Button ID="btnPrev" runat="server" Text="<" οnclick="btnPrev_Click" />
<asp:Button ID="btnNext" runat="server" Text=">" οnclick="btnNext_Click" />
<asp:Button ID="btnLast" runat="server" Text=">|" οnclick="btnLast_Click" />
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:TextBox ID="txtNumber" runat="server" Width="24px"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="只能是数字且大于最小值" ControlToCompare="TextBox1"
ControlToValidate="txtNumber" ForeColor="Red" Operator="GreaterThan"
Type="Integer" Display="Dynamic" ValidationGroup="panDuan"></asp:CompareValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server"
ErrorMessage="只能是数字且小于最大值" ControlToCompare="TextBox2"
ControlToValidate="txtNumber" Display="Dynamic" ForeColor="Red"
Operator="GreaterThanEqual" ValidationGroup="panDuan"></asp:CompareValidator>
<asp:Button ID="btnGo" runat="server" Text="GO" οnclick="btnGo_Click"
ValidationGroup="panDuan" />
<br />
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:HiddenField ID="HiddenField2" runat="server" />
<br />
<asp:TextBox ID="TextBox1" runat="server" Visible="False"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Visible="False"></asp:TextBox>
</div>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindStudent(1);
}
}
private void BindStudent(int index)
{
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_Student_Select_by_Page_rowNumber";
cmd.Parameters.AddWithValue("@pageSize", 3);
cmd.Parameters.AddWithValue("@pageIndex", index);
cmd.Parameters.Add("@pageCount", System.Data.SqlDbType.Int).Direction = ParameterDirection.Output;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
string pageCount = cmd.Parameters["@pageCount"].Value.ToString();
Label1.Text = "当前第"+index.ToString()+"页共"+pageCount+"页";
HiddenField1.Value = index.ToString();
HiddenField2.Value = pageCount;
TextBox1.Text = (index+1).ToString();
TextBox2.Text = (int.Parse(pageCount)+1).ToString();
}
}
}
protected void btnFirst_Click(object sender, EventArgs e)
{
BindStudent(1);
}
protected void btnPrev_Click(object sender, EventArgs e)
{
int index = int.Parse(HiddenField1.Value);
if (index > 1)
{
index--;
BindStudent(index);
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
int index = int.Parse(HiddenField1.Value);
int pageCount = int.Parse(HiddenField2.Value);
if (index<pageCount)
{
index++;
BindStudent(index);
}
}
protected void btnLast_Click(object sender, EventArgs e)
{
BindStudent(int.Parse(HiddenField2.Value));
}
protected void btnGo_Click(object sender, EventArgs e)
{
BindStudent(int.Parse(txtNumber.Text));
}
2.实现手动排序,不适用gridview中的分页。
前台代码:
<div>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" onsorting="GridView1_Sorting">
<Columns>
<asp:BoundField DataField="sid" HeaderText="编号" SortExpression="sid desc" />
<asp:BoundField DataField="sname" HeaderText="姓名" SortExpression="sname" />
<asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age asc" />
</Columns>
</asp:GridView>
</div>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindStudent("sid");
}
}
private void bindStudent(string sort)
{
string constr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = con.CreateCommand())
{
string sql = "select * from student order by " + sort;//有漏洞,和使用参数的方式解决。
cmd.CommandText = sql;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
this.GridView1.DataSource = dt;
}
}
this.DataBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
bindStudent(e.SortExpression);
}
3.参考代码实现全选、全不选(选作。提示使用模板页,其中加入checkbox控件)
前台代码:
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
DeleteCommand="DELETE FROM [student] WHERE [sid] = @sid"
InsertCommand="INSERT INTO [student] ([sname], [classid], [sex], [age], [isking], [photo]) VALUES (@sname, @classid, @sex, @age, @isking, @photo)"
SelectCommand="SELECT * FROM [student]"
UpdateCommand="UPDATE [student] SET [sname] = @sname, [classid] = @classid, [sex] = @sex, [age] = @age, [isking] = @isking, [photo] = @photo WHERE [sid] = @sid">
<DeleteParameters>
<asp:Parameter Name="sid" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="classid" Type="Int32" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Byte" />
<asp:Parameter Name="isking" Type="Boolean" />
<asp:Parameter Name="photo" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="classid" Type="Int32" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Byte" />
<asp:Parameter Name="isking" Type="Boolean" />
<asp:Parameter Name="photo" Type="String" />
<asp:Parameter Name="sid" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="sid" DataSourceID="SqlDataSource1" Width="355px">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True"
oncheckedchanged="CheckBox2_CheckedChanged" />
<input id="Checkbox3" type="checkbox" οnclick="change(this)" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="sid" HeaderText="sid" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:BoundField DataField="sname" HeaderText="sname" SortExpression="sname" />
<asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
<asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="删除"
Width="127px" />
</div>
后台代码:
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
CheckBox ckb = sender as CheckBox;
foreach (GridViewRow row in this.GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
(row.Cells[0].FindControl("CheckBox1") as CheckBox).Checked = ckb.Checked;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in this.GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox ckb = row.Cells[0].FindControl("CheckBox1") as CheckBox;
if (ckb.Checked)
{
Response.Write(row.Cells[1].Text + "<br/>");
}
}
}
}
4.将上一课的模版列功能改造成博客内容列表, 并使用存储过程完成分页。
前台代码:
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="id" HeaderText="行号" />
<asp:BoundField DataField="sid" HeaderText="编号" />
<asp:HyperLinkField DataNavigateUrlFields="photo"
DataNavigateUrlFormatString="showImage.aspx?photourl={0}" DataTextField="sname"
DataTextFormatString="{0}" HeaderText="姓名" />
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:BoundField DataField="age" HeaderText="年龄" />
<asp:ImageField DataImageUrlField="photo" HeaderText="照片">
<ControlStyle Height="60px" Width="60px" />
</asp:ImageField>
</Columns>
</asp:GridView>
<asp:Button ID="btnFirst" runat="server" Text="|<"
οnclick="btnFirst_Click" />
<asp:Button ID="btnPrev" runat="server" Text="<" οnclick="btnPrev_Click" />
<asp:Button ID="btnNext" runat="server" Text=">" οnclick="btnNext_Click" />
<asp:Button ID="btnLast" runat="server" Text=">|" οnclick="btnLast_Click" />
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:TextBox ID="txtNumber" runat="server" Width="24px"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="只能是数字且大于最小值" ControlToCompare="TextBox1"
ControlToValidate="txtNumber" ForeColor="Red" Operator="GreaterThan"
Type="Integer" Display="Dynamic" ValidationGroup="panDuan"></asp:CompareValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server"
ErrorMessage="只能是数字且小于最大值" ControlToCompare="TextBox2"
ControlToValidate="txtNumber" Display="Dynamic" ForeColor="Red"
Operator="GreaterThanEqual" ValidationGroup="panDuan"></asp:CompareValidator>
<asp:Button ID="btnGo" runat="server" Text="GO" οnclick="btnGo_Click"
ValidationGroup="panDuan" />
<br />
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:HiddenField ID="HiddenField2" runat="server" />
<br />
<asp:TextBox ID="TextBox1" runat="server" Visible="False"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Visible="False"></asp:TextBox>
</div>
后台代码:
public partial class boKe : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindStudent(1);
}
}
private void BindStudent(int index)
{
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_Student_Select_by_Page_rowNumber";
cmd.Parameters.AddWithValue("@pageSize", 3);
cmd.Parameters.AddWithValue("@pageIndex", index);
cmd.Parameters.Add("@pageCount", System.Data.SqlDbType.Int).Direction = ParameterDirection.Output;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
string pageCount = cmd.Parameters["@pageCount"].Value.ToString();
Label1.Text = "当前第" + index.ToString() + "页共" + pageCount + "页";
HiddenField1.Value = index.ToString();
HiddenField2.Value = pageCount;
TextBox1.Text = (index + 1).ToString();
TextBox2.Text = (int.Parse(pageCount) + 1).ToString();
}
}
}
protected void btnFirst_Click(object sender, EventArgs e)
{
BindStudent(1);
}
protected void btnPrev_Click(object sender, EventArgs e)
{
int index = int.Parse(HiddenField1.Value);
if (index > 1)
{
index--;
BindStudent(index);
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
int index = int.Parse(HiddenField1.Value);
int pageCount = int.Parse(HiddenField2.Value);
if (index < pageCount)
{
index++;
BindStudent(index);
}
}
protected void btnLast_Click(object sender, EventArgs e)
{
BindStudent(int.Parse(HiddenField2.Value));
}
protected void btnGo_Click(object sender, EventArgs e)
{
BindStudent(int.Parse(txtNumber.Text));
}
}
前台代码:
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="id" HeaderText="行号" />
<asp:BoundField DataField="sid" HeaderText="编号" />
<asp:BoundField DataField="sname" HeaderText="姓名" />
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:BoundField DataField="age" HeaderText="年龄" />
</Columns>
</asp:GridView>
<asp:Button ID="btnFirst" runat="server" Text="|<"
οnclick="btnFirst_Click" />
<asp:Button ID="btnPrev" runat="server" Text="<" οnclick="btnPrev_Click" />
<asp:Button ID="btnNext" runat="server" Text=">" οnclick="btnNext_Click" />
<asp:Button ID="btnLast" runat="server" Text=">|" οnclick="btnLast_Click" />
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:TextBox ID="txtNumber" runat="server" Width="24px"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="只能是数字且大于最小值" ControlToCompare="TextBox1"
ControlToValidate="txtNumber" ForeColor="Red" Operator="GreaterThan"
Type="Integer" Display="Dynamic" ValidationGroup="panDuan"></asp:CompareValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server"
ErrorMessage="只能是数字且小于最大值" ControlToCompare="TextBox2"
ControlToValidate="txtNumber" Display="Dynamic" ForeColor="Red"
Operator="GreaterThanEqual" ValidationGroup="panDuan"></asp:CompareValidator>
<asp:Button ID="btnGo" runat="server" Text="GO" οnclick="btnGo_Click"
ValidationGroup="panDuan" />
<br />
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:HiddenField ID="HiddenField2" runat="server" />
<br />
<asp:TextBox ID="TextBox1" runat="server" Visible="False"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Visible="False"></asp:TextBox>
</div>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindStudent(1);
}
}
private void BindStudent(int index)
{
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_Student_Select_by_Page_rowNumber";
cmd.Parameters.AddWithValue("@pageSize", 3);
cmd.Parameters.AddWithValue("@pageIndex", index);
cmd.Parameters.Add("@pageCount", System.Data.SqlDbType.Int).Direction = ParameterDirection.Output;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
string pageCount = cmd.Parameters["@pageCount"].Value.ToString();
Label1.Text = "当前第"+index.ToString()+"页共"+pageCount+"页";
HiddenField1.Value = index.ToString();
HiddenField2.Value = pageCount;
TextBox1.Text = (index+1).ToString();
TextBox2.Text = (int.Parse(pageCount)+1).ToString();
}
}
}
protected void btnFirst_Click(object sender, EventArgs e)
{
BindStudent(1);
}
protected void btnPrev_Click(object sender, EventArgs e)
{
int index = int.Parse(HiddenField1.Value);
if (index > 1)
{
index--;
BindStudent(index);
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
int index = int.Parse(HiddenField1.Value);
int pageCount = int.Parse(HiddenField2.Value);
if (index<pageCount)
{
index++;
BindStudent(index);
}
}
protected void btnLast_Click(object sender, EventArgs e)
{
BindStudent(int.Parse(HiddenField2.Value));
}
protected void btnGo_Click(object sender, EventArgs e)
{
BindStudent(int.Parse(txtNumber.Text));
}
2.实现手动排序,不适用gridview中的分页。
前台代码:
<div>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" onsorting="GridView1_Sorting">
<Columns>
<asp:BoundField DataField="sid" HeaderText="编号" SortExpression="sid desc" />
<asp:BoundField DataField="sname" HeaderText="姓名" SortExpression="sname" />
<asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age asc" />
</Columns>
</asp:GridView>
</div>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindStudent("sid");
}
}
private void bindStudent(string sort)
{
string constr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = con.CreateCommand())
{
string sql = "select * from student order by " + sort;//有漏洞,和使用参数的方式解决。
cmd.CommandText = sql;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
this.GridView1.DataSource = dt;
}
}
this.DataBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
bindStudent(e.SortExpression);
}
3.参考代码实现全选、全不选(选作。提示使用模板页,其中加入checkbox控件)
前台代码:
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
DeleteCommand="DELETE FROM [student] WHERE [sid] = @sid"
InsertCommand="INSERT INTO [student] ([sname], [classid], [sex], [age], [isking], [photo]) VALUES (@sname, @classid, @sex, @age, @isking, @photo)"
SelectCommand="SELECT * FROM [student]"
UpdateCommand="UPDATE [student] SET [sname] = @sname, [classid] = @classid, [sex] = @sex, [age] = @age, [isking] = @isking, [photo] = @photo WHERE [sid] = @sid">
<DeleteParameters>
<asp:Parameter Name="sid" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="classid" Type="Int32" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Byte" />
<asp:Parameter Name="isking" Type="Boolean" />
<asp:Parameter Name="photo" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="classid" Type="Int32" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Byte" />
<asp:Parameter Name="isking" Type="Boolean" />
<asp:Parameter Name="photo" Type="String" />
<asp:Parameter Name="sid" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="sid" DataSourceID="SqlDataSource1" Width="355px">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True"
oncheckedchanged="CheckBox2_CheckedChanged" />
<input id="Checkbox3" type="checkbox" οnclick="change(this)" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="sid" HeaderText="sid" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:BoundField DataField="sname" HeaderText="sname" SortExpression="sname" />
<asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
<asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="删除"
Width="127px" />
</div>
后台代码:
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
CheckBox ckb = sender as CheckBox;
foreach (GridViewRow row in this.GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
(row.Cells[0].FindControl("CheckBox1") as CheckBox).Checked = ckb.Checked;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in this.GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox ckb = row.Cells[0].FindControl("CheckBox1") as CheckBox;
if (ckb.Checked)
{
Response.Write(row.Cells[1].Text + "<br/>");
}
}
}
}
4.将上一课的模版列功能改造成博客内容列表, 并使用存储过程完成分页。
前台代码:
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="id" HeaderText="行号" />
<asp:BoundField DataField="sid" HeaderText="编号" />
<asp:HyperLinkField DataNavigateUrlFields="photo"
DataNavigateUrlFormatString="showImage.aspx?photourl={0}" DataTextField="sname"
DataTextFormatString="{0}" HeaderText="姓名" />
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:BoundField DataField="age" HeaderText="年龄" />
<asp:ImageField DataImageUrlField="photo" HeaderText="照片">
<ControlStyle Height="60px" Width="60px" />
</asp:ImageField>
</Columns>
</asp:GridView>
<asp:Button ID="btnFirst" runat="server" Text="|<"
οnclick="btnFirst_Click" />
<asp:Button ID="btnPrev" runat="server" Text="<" οnclick="btnPrev_Click" />
<asp:Button ID="btnNext" runat="server" Text=">" οnclick="btnNext_Click" />
<asp:Button ID="btnLast" runat="server" Text=">|" οnclick="btnLast_Click" />
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:TextBox ID="txtNumber" runat="server" Width="24px"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="只能是数字且大于最小值" ControlToCompare="TextBox1"
ControlToValidate="txtNumber" ForeColor="Red" Operator="GreaterThan"
Type="Integer" Display="Dynamic" ValidationGroup="panDuan"></asp:CompareValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server"
ErrorMessage="只能是数字且小于最大值" ControlToCompare="TextBox2"
ControlToValidate="txtNumber" Display="Dynamic" ForeColor="Red"
Operator="GreaterThanEqual" ValidationGroup="panDuan"></asp:CompareValidator>
<asp:Button ID="btnGo" runat="server" Text="GO" οnclick="btnGo_Click"
ValidationGroup="panDuan" />
<br />
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:HiddenField ID="HiddenField2" runat="server" />
<br />
<asp:TextBox ID="TextBox1" runat="server" Visible="False"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Visible="False"></asp:TextBox>
</div>
后台代码:
public partial class boKe : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindStudent(1);
}
}
private void BindStudent(int index)
{
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_Student_Select_by_Page_rowNumber";
cmd.Parameters.AddWithValue("@pageSize", 3);
cmd.Parameters.AddWithValue("@pageIndex", index);
cmd.Parameters.Add("@pageCount", System.Data.SqlDbType.Int).Direction = ParameterDirection.Output;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
string pageCount = cmd.Parameters["@pageCount"].Value.ToString();
Label1.Text = "当前第" + index.ToString() + "页共" + pageCount + "页";
HiddenField1.Value = index.ToString();
HiddenField2.Value = pageCount;
TextBox1.Text = (index + 1).ToString();
TextBox2.Text = (int.Parse(pageCount) + 1).ToString();
}
}
}
protected void btnFirst_Click(object sender, EventArgs e)
{
BindStudent(1);
}
protected void btnPrev_Click(object sender, EventArgs e)
{
int index = int.Parse(HiddenField1.Value);
if (index > 1)
{
index--;
BindStudent(index);
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
int index = int.Parse(HiddenField1.Value);
int pageCount = int.Parse(HiddenField2.Value);
if (index < pageCount)
{
index++;
BindStudent(index);
}
}
protected void btnLast_Click(object sender, EventArgs e)
{
BindStudent(int.Parse(HiddenField2.Value));
}
protected void btnGo_Click(object sender, EventArgs e)
{
BindStudent(int.Parse(txtNumber.Text));
}
}