我使用了gridview的分页。但在我的一个项目中,我想添加分页 上一页1 2 3 ..下一步 我有什么方法可以做同样的事情
我知道数字和之前的下一个,但它是两者的组合
答案 0 :(得分:2)
这将需要手动编码,因为目前没有内置功能可以将寻呼机设为:Previous 1 2 3.. Next
通过 BLEERMAKERS 的礼貌,令我着迷的一个非常好的方法是创建一个自定义控件来实现这一目标。在此发布基本代码以便开始。
public class CustomGridView : GridView
{
CustomGridView grd;
protected void PrevNextClick(object sender, System.Web.UI.WebControls.CommandEventArgs e)
{
if (e.CommandName == "PREV") {grd.PageIndex += -1;}
else { grd.PageIndex += 1;}
GridViewPageEventArgs gvpea = new GridViewPageEventArgs(grd.PageIndex);
grd.OnPageIndexChanging(gvpea);
}
protected override void OnRowCreated(GridViewRowEventArgs e)
{
base.OnRowCreated(e);
if (e.Row.RowType == DataControlRowType.Pager)
{
Table pagerTable = (Table)e.Row.Cells[0].Controls[0];
grd = this;
TableRow pagerRow = pagerTable.Rows[0];
PagerSettings pagerSettings = grd.PagerSettings;
int cellsCount = pagerRow.Cells.Count;
if (pagerSettings.Mode == PagerButtons.Numeric || pagerSettings.Mode == PagerButtons.NumericFirstLast)
{
//check whether previous button exists
LinkButton btnPrev = new LinkButton();
btnPrev.Text = pagerSettings.PreviousPageText;
btnPrev.CommandName = "PREV";
if (grd.PageIndex <= 0) btnPrev.Visible = false;
btnPrev.Command += PrevNextClick;
TableCell PrevCell = new TableCell();
PrevCell.Controls.Add(btnPrev);
pagerRow.Cells.AddAt(0, PrevCell);
//check whether previous button exists
LinkButton btnNext = new LinkButton();
btnNext.Text = pagerSettings.NextPageText;
btnNext.CommandName = "NEXT";
if (grd.PageIndex >= grd.PageCount - 1) btnNext.Visible = false;
btnNext.Command += PrevNextClick;
TableCell NextCell = new TableCell();
NextCell.Controls.Add(btnNext);
pagerRow.Cells.Add(NextCell);
}
}
}
}
将此添加到您的web.config中(假设上面的代码在app_code中,您也可以使用汇编版本):
<system.web>
<pages>
<controls>
<add namespace="CustomControls" tagPrefix="CC" />
</controls>
</pages>
</system.web>
在.aspx文件中使用CustomGridView:
<CC:CustomGridView ID="CustomGridView1"
runat="server"
AllowPaging="True"
DataSourceID="DataSource1"
PageSize="10"
PagerSettings-NextPageText="Next page"
PagerSettings-PreviousPageText="Previous page"
PagerSettings-Mode="NumericFirstLast"
/>
Read on here获得完整的创意。