1

我使用了gridview的分页。但是在我的一个项目中,我想添加分页,例如Previous 1 2 3 .. Next 有什么方法可以做同样的事情

我知道数字和上一个,但它是两者的结合

4

1 回答 1

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"
/>

继续阅读这里以获得完整的想法。

于 2013-08-14T04:56:41.567 回答