如何在datalist中添加分页

时间:2017-02-11 11:26:45

标签: c# asp.net datalist

我已经为一个数据列表实现了分页,但它没有显示正确的分页。

please review this image.

你可以看到这个图像中的输出它没有正确显示输出,我需要的输出是1到10的编号,之后就像这样,所以回复一下我的问题的解决方案是什么

                                    <asp:DataList ID="dtlpaging" runat="server" RepeatDirection="Horizontal" OnItemDataBound="dtlpaging_ItemDataBound" >
                                        <ItemTemplate>

                                                        <asp:LinkButton ID="lnkbtnPaging" runat="server" OnClick="lnkbtnPaging_Click" Text='<%# Eval("pagetext") %>'
                                                            ValidationGroup='<%# Eval("pageindex") %>' CommandArgument='<%# Eval("pageindex") %>'
                                                            CommandName="Paging" CssClass="btn btn-info" style="padding:1px 7px;"></asp:LinkButton>

                                        </ItemTemplate>
                                    </asp:DataList>

///////////////////// c#coding ///////////////

if (ViewState["Filterrecords"] == "")
    {
        DataTable dset = new DataTable();
        dset = dataget();
        if (dset.Rows.Count > 0)
        {
            dlouter.Visible = true;
            lblrecord.Visible = false;
            adsource = new PagedDataSource();
            adsource.DataSource = dset.DefaultView;
        }
        else
        {
            dlouter.Visible = false;
            lblrecord.Visible = true;
            adsource = new PagedDataSource();
            adsource.DataSource = dset.DefaultView;

        }
    }
    else
    {
        DataTable dt = new DataTable();
        dt = (DataTable)ViewState["Filterrecords"];
        if (dt.Rows.Count > 0)
        {
            dlouter.Visible = true;
            lblrecord.Visible = false;
            adsource = new PagedDataSource();
            adsource.DataSource = dt.DefaultView;
        }
        else
        {
            dlouter.Visible = false;
            lblrecord.Visible = true;
            adsource = new PagedDataSource();
            adsource.DataSource = dt.DefaultView;
        }
    }
    adsource.PageSize = 10;
    adsource.AllowPaging = true;
    adsource.CurrentPageIndex = pos;
    imgbtnfirst.Enabled = !adsource.IsFirstPage;
    imgbtnprevious.Enabled = !adsource.IsFirstPage;
    imgbtnlast.Enabled = !adsource.IsLastPage;
    imgbtnnext.Enabled = !adsource.IsLastPage;
    dopaging();
    dlouter.DataSource = adsource;
    dlouter.DataBind();

 protected void dtlpaging_ItemDataBound(object sender, DataListItemEventArgs e)
{
    LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");

    pos = (int)this.ViewState["vs"];
    string a = Convert.ToString(pos);

    if (lnkbtnPage.CommandArgument.ToString() == a.ToString())
    {
        lnkbtnPage.Enabled = false;
        lnkbtnPage.Style.Add("font-family", "Arial");
        lnkbtnPage.Style.Add("font-size", "11px");
        lnkbtnPage.Style.Add("color", "#000000");
        lnkbtnPage.Font.Bold = true;

    }
}
public void dopaging()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("pageindex");
    dt.Columns.Add("pagetext");
    for (int i = 0; i < adsource.PageCount; i++)
    {
        DataRow dr = dt.NewRow();
        dr[0] = i;
        dr[1] = i + 1;
        dt.Rows.Add(dr);
    }
    dtlpaging.DataSource = dt;
    dtlpaging.DataBind();
    dtlpaging1.DataSource = dt;
    dtlpaging1.DataBind();
}
 protected void lnkbtnPaging_Click(object sender, EventArgs e)
{
    int id = Convert.ToInt32(((LinkButton)sender).ValidationGroup.ToString());
    pos = id;
    this.ViewState["vs"] = pos;
    databind();
}

1 个答案:

答案 0 :(得分:0)

_PageDataSource.DataSource = dset.DefaultView;
_PageDataSource.AllowPaging = true;
_PageDataSource.PageSize = 10;
_PageDataSource.CurrentPageIndex = CurrentPage;
ViewState["totalpages"] = _PageDataSource.PageCount;
if (_PageDataSource.PageCount > 1)
{
    //paPageing.Visible = true;
    this.imgbtnprevious.Enabled = !_PageDataSource.IsFirstPage;
    this.imgbtnnext.Enabled = !_PageDataSource.IsLastPage;
    this.imgbtnfirst.Enabled = !_PageDataSource.IsFirstPage;
    this.imgbtnlast.Enabled = !_PageDataSource.IsLastPage;
}
else
{
    //paPageing.Visible = false;
}
this.dlouter.DataSource = _PageDataSource;
this.dlouter.DataBind();
this.doPaging();

 private void doPaging()
{
DataTable dt = new DataTable();
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageText");

fistIndex = CurrentPage - 5;


if (CurrentPage > 5)
{
    lastIndex = CurrentPage + 5;
}
else
{
    lastIndex = 10;
}
if (lastIndex > Convert.ToInt32(ViewState["totalpages"]))
{
    lastIndex = Convert.ToInt32(ViewState["totalpages"]);
    fistIndex = lastIndex - 10;
}

if (fistIndex < 0)
{
    fistIndex = 0;
}

for (int i = fistIndex; i < lastIndex; i++)
{
    DataRow dr = dt.NewRow();
    dr[0] = i;
    dr[1] = i + 1;
    dt.Rows.Add(dr);
}

this.dtlpaging.DataSource = dt;
this.dtlpaging.DataBind();
this.dtlpaging1.DataSource = dt;
this.dtlpaging1.DataBind();
 }
 #region Private Properties
 private int CurrentPage
{
get
{
    object objPage = ViewState["_CurrentPage"];
    int _CurrentPage = 0;
    if (objPage == null)
    {
        _CurrentPage = 0;
    }
    else
    {
        _CurrentPage = (int)objPage;
    }
    return _CurrentPage;
}
set { ViewState["_CurrentPage"] = value; }
}
private int fistIndex
{
   get
   {

    int _FirstIndex = 0;
    if (ViewState["_FirstIndex"] == null)
    {
        _FirstIndex = 0;
    }
    else
    {
        _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
    }
    return _FirstIndex;
}
set { ViewState["_FirstIndex"] = value; }
}
 private int lastIndex
  {
    get
{

    int _LastIndex = 0;
    if (ViewState["_LastIndex"] == null)
    {
        _LastIndex = 0;
    }
    else
    {
        _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
    }
    return _LastIndex;
}
set { ViewState["_LastIndex"] = value; }
}
#endregion
#region PagedDataSource
PagedDataSource _PageDataSource = new PagedDataSource();
#endregion
 protected void imgbtnfirst_Click(object sender, ImageClickEventArgs e)
{
CurrentPage = 0;
this.databind();
}
protected void imgbtnprevious_Click(object sender, ImageClickEventArgs e)
{
CurrentPage -= 1;
this.databind();
}
protected void dtlpaging_ItemDataBound(object sender, DataListItemEventArgs e)
{
LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
{
    lnkbtnPage.Enabled = false;
    lnkbtnPage.CssClass = "pageing_l_active";
}
}

protected void imgbtnnext_Click(object sender, ImageClickEventArgs e)
{
CurrentPage += 1;
this.databind();
}
protected void imgbtnlast_Click(object sender, ImageClickEventArgs e)
{
CurrentPage = (Convert.ToInt32(this.ViewState["totalpages"]) - 1);
this.databind();
}
protected void dtlpaging_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("Paging"))
{
    CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
    this.databind();
}
}