locked
Dalist pageing RRS feed

  • Question

  • User66371569 posted

    How can I page my data list  and place buttons next previous 

    Thank you

    Sunday, October 21, 2018 10:43 AM

All replies

  • User-369506445 posted

    hi

    here is a good sample, please follow

    https://www.aspsnippets.com/Articles/Implement-Paging-in-DataList-control-in-ASPNet.aspx

    Sunday, October 21, 2018 11:12 AM
  • User839733648 posted

    Hi thepast,

    You could set paging when you bind the datas with your datalist.

    I've made a simple demo on my side, and you may refer to it.

    .Aspx

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <table>
                    <asp:DataList ID="DataList1" runat="server">
                        <HeaderTemplate>
                            <h1>Infomation</h1>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <tr>
                                <td>
                                    <h4>Eid</h4>
                                </td>
                                <td>
                                    <h4>Ename</h4>
                                </td>
                                <td>
                                    <h4>Age</h4>
                                </td>
                                <td>
                                    <h4>Sex</h4>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <h5><%#Eval("Eid") %></h5>
                                </td>
                                <td>
                                    <h5><%#Eval("Ename") %></h5>
                                </td>
                                <td>
                                    <h5><%#Eval("age") %></h5>
                                </td>
                                <td>
                                    <h5><%#Eval("sex") %></h5>
                                </td>
                            </tr>
                        </ItemTemplate>
                    </asp:DataList>
                </table>
                <table>
                    <tr>
                        <td>
                            <asp:Button ID="btnfirst" runat="server" Font-Bold="true" Text="<<" OnClick="btnfirst_Click" /></td>
                        <td>
                            <asp:Button ID="btnprevious" runat="server" Font-Bold="true" Text="<" OnClick="btnprevious_Click" /></td>
                        <td>
                            <asp:Button ID="btnnext" runat="server" Font-Bold="true" Text=">" OnClick="btnnext_Click" /></td>
                        <td>
                            <asp:Button ID="btnlast" runat="server" Font-Bold="true" Text=">>" OnClick="btnlast_Click" /></td>
                    </tr>
                </table>
            </div>
        </form>
    </body>
    </html>
    

    Code-behind.

            string connstring = ConfigurationManager.ConnectionStrings["EmployeeManagementConnectionString"].ConnectionString;
            SqlDataAdapter dadapter;
            DataSet dset;
            PagedDataSource adsource;
            int pos;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    this.ViewState["vs"] = 0;
                }
                pos = (int)this.ViewState["vs"];
                databind();
            }
            public void databind()
            {
                dadapter = new SqlDataAdapter("select * from tb_info", connstring);
                dset = new DataSet();
                adsource = new PagedDataSource();
                dadapter.Fill(dset);
                adsource.DataSource = dset.Tables[0].DefaultView;
                //set paging
                adsource.PageSize = 3;
                adsource.AllowPaging = true;
                adsource.CurrentPageIndex = pos;
                btnfirst.Enabled = !adsource.IsFirstPage;
                btnprevious.Enabled = !adsource.IsFirstPage;
                btnlast.Enabled = !adsource.IsLastPage;
                btnnext.Enabled = !adsource.IsLastPage;
                DataList1.DataSource = adsource;
                DataList1.DataBind();
            }
    
            protected void btnfirst_Click(object sender, EventArgs e)
            {
                pos = 0;
                databind();
            }
    
            protected void btnprevious_Click(object sender, EventArgs e)
            {
                pos = (int)this.ViewState["vs"];
                pos -= 1;
                this.ViewState["vs"] = pos;
                databind();
            }
    
            protected void btnnext_Click(object sender, EventArgs e)
            {
                pos = (int)this.ViewState["vs"];
                pos += 1;
                this.ViewState["vs"] = pos;
                databind();
            }
    
            protected void btnlast_Click(object sender, EventArgs e)
            {
                pos = adsource.PageCount - 1;
                databind();
            }

    result:

    Besides, the official document about paging data in a DataList you may refer to: Paging Report Data in a DataList or Repeater Control 

    Best Regards,

    Jenifer

    Monday, October 22, 2018 6:08 AM