//利用自定义控件,做分页比较省事,AspNetPager1控件。
//该控件需要设置一些属性如下:
<webdiyer:AspNetPager style="FONT-SIZE: 14px" id="AspNetPager1" runat="server" horizontalalign="Center" onpagechanged="AspNetPager1_PageChanged" showcustominfosection="Left" meta:resourceKey="AspNetPager1" InputBoxStyle="width:19px" CustomInfoHTML="第<font color='red'><b>%CurrentPageIndex%</b></font>页共%PageCount% 页 %StartRecordIndex%-%EndRecordIndex%" ShowNavigationToolTip="True" CustomInfoTextAlign="Center" ShowInputBox="Always" FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PageSize="4" PrevPageText="前一页" CssClass="formfield" SubmitButtonClass="formfield" SubmitButtonText="GO" CustomInfoClass="formbutton" BackColor="WhiteSmoke" NumericButtonCount="5">
</webdiyer:AspNetPager>
//后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetBookByCId(); }
}
private void GetBookByCId() { SqlConnection con = new SqlConnection("server=.;database=db_GoodsManage;user id=sa;pwd=yxqznkl"); string sql = "select GoodsID,DepotName,GoodsName,StockNum,GoodsUnit,SellPrice from tb_Stock"; SqlDataAdapter sdr = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); sdr.Fill(ds,"tb"); PagedDataSource PDS = new PagedDataSource(); AspNetPager1.RecordCount = ds.Tables["tb"].DefaultView.Count;
PDS.DataSource = ds.Tables["tb"].DefaultView; PDS.AllowPaging = true; PDS.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1; PDS.PageSize = AspNetPager1.PageSize; GridView1.DataSource = PDS; DataBind(); }
protected void AspNetPager1_PageChanged(object sender, EventArgs e) { GetBookByCId(); }
