GridView + AspNetPager AspNetPager不显示!
<webdiyer:AspNetPager ID="AspNetPager2" runat="server" CustomInfoHTML="第<font color='red'><b>% CurrentPageIndex%</b></font>页 共:%PageCount%页 %StartRecordIndex%-%EndRecordIndex%"
CustomInfoTextAlign="Center" FirstPageText="【首页】" Height="25px" HorizontalAlign="Center"
InputBoxStyle="width:19px" LastPageText="【尾页】" NextPageText="【下页】"
PrevPageText="【前页】 " ShowCustomInfoSection="Left" ShowInputBox="Never" ShowNavigationToolTip="True"
Width="691px" style="font-size: 9pt" OnPageChanging="AspNetPager2_PageChanging" PageSize="20">
</webdiyer:AspNetPager>
public void Bind()
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
con.Open();
string sql = "select * from stationery where count>0";
SqlCommand com = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds, "stationery");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
AspNetPager2.RecordCount = pds.Count;
pds.CurrentPageIndex = AspNetPager2.CurrentPageIndex - 1;
pds.PageSize = AspNetPager2.PageSize;
this.GridView1.DataSource = pds;
this.GridView1.DataBind();
}
protected void AspNetPager2_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
this.AspNetPager2.CurrentPageIndex = e.NewPageIndex;
Bind();
}
怎么 AspNetPager 在页面上都不显示呀
------解决方案--------------------AspNetPager2.RecordCount = pds.Count;
这个值必须先于数据绑定之前,并且不能和数据绑定在同一个过程中,因为分页控件要根据这个值计算总页数,把你的代码改成这样:
public void Bind()
{ AspNetPager2.RecordCount = 这里必须有实际的总记录数,不能和下面的PagedDataSource同时获取结果,这样就太迟了。
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
con.Open();
string sql = "select * from stationery where count>0";
SqlCommand com = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds, "stationery");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.CurrentPageIndex = AspNetPager2.CurrentPageIndex - 1;
pds.PageSize = AspNetPager2.PageSize;
this.GridView1.DataSource = pds;
this.GridView1.DataBind();
}
------解决方案--------------------C# code
string sqlSelect = "select * from News order by addtime desc";
SQLcon sl = new SQLcon();
SqlDataAdapter sda;
DataSet ds = new DataSet();
sda = new SqlDataAdapter(sqlSelect, sl.Connection);
sda.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "News");
sl.ConOpen();
AspNetPager1.RecordCount =ds.Tables.Count;
if (ds.Tables[0].Rows.Count