分页功能的实现

时间:2021-04-03 13:26:00


利用Repeater实现分页功能


 public class DB
{
public static SqlConnection createConnection()
{
SqlConnection con = new SqlConnection("server=.;database=vote;User ID=sa;Password=123456");
return con;
}
}


  protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
this.Label3.Text = "1";
this.dataBindToRepeater();
}
}

 private void dataBindToRepeater()
{
int curPage = Convert.ToInt32(this.Label3 .Text); //变量当前页
SqlConnection con = DB.createConnection();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from voteMaster", con);
DataSet ds = new DataSet();
sda.Fill(ds, "voteID");
System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource(); //实现分页功能
ps.DataSource = ds.Tables["voteID"].DefaultView;
ps.AllowPaging = true; //允许分页
ps.PageSize = 2; //每页数据有2条
ps.CurrentPageIndex = curPage - 1; //当前页数
this.Button3.Enabled = true;
this.Button4.Enabled = true;
if (curPage ==1)
{
this.Button3.Enabled = false; //上一页按钮不能用
}
if (curPage ==ps.PageCount )
{
this.Button4.Enabled = false; //下一页按钮不能用
}
this.Repeater1.DataSource = ps;
this.Repeater1.DataBind();
}

 /// <summary>
/// 下一页功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button4_Click(object sender, EventArgs e)
{
this.Label3.Text = (Convert.ToInt32(this.Label3.Text) + 1).ToString();
this.dataBindToRepeater();
}
/// <summary>
/// 上一页功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button3_Click(object sender, EventArgs e)
{
this.Label3.Text = (Convert.ToInt32 (this.Label3.Text)-1).ToString() ;
this.dataBindToRepeater();
}
}

<asp:Panel ID="Panel1" runat="server" Height="293px" Width="383px">
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate >
<%# DataBinder .Eval (Container .DataItem,"voteID") %><%# DataBinder .Eval (Container .DataItem,"voteTitle") %>
<br />
</ItemTemplate>
<AlternatingItemTemplate >
<font color="blue"><%# DataBinder .Eval (Container .DataItem,"voteID") %><%# DataBinder .Eval (Container .DataItem,"voteTitle") %>
<br />
</font>
</AlternatingItemTemplate>
<HeaderTemplate >
<h3>模板页眉</h3>
</HeaderTemplate>
<FooterTemplate >
<h3>模板页脚</h3>
</FooterTemplate>
<SeparatorTemplate >
<hr color="blue" size="1" />
</SeparatorTemplate>
</asp:Repeater>
</asp:Panel>


最终实现效果:


分页功能的实现


利用GridView实现分页功能


将属性AllowPaging改为TRUE,PageSize自己设定一个数(我设定的为2)

 protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
private void Bind()
{
SqlConnection con = DB.createConnection();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from voteMaster", con);
DataSet ds = new DataSet();
sda.Fill(ds, "voteID");
this.GridView1.DataSource = ds.Tables["voteID"];
this.GridView1.DataBind();
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Bind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}

最终效果:

分页功能的实现