Click here to Skip to main content
15,886,593 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more: , +
Hi,

I am using datalist to display some items in asp.net using c#. Is there any method to implement paging in datalist?

Thanks...
Posted
Comments
infosoftsaranya 16-Mar-12 7:25am    
drwerwer
♥…ЯҠ…♥ 9-Nov-13 6:40am    
Testing in Code project ;-)
infosoftsaranya 16-Mar-12 7:25am    
rtertetrtet

 
Share this answer
 
A quick Google search gave me these links:
Paging of Data list[^]
DataList Custom Paging in ASP.NET using C#[^]

Have a look at them and see if they help.
 
Share this answer
 
C#
namespace PagininDataLista
{
    public partial class Display_question : System.Web.UI.Page
    {
        string ansid;
        string querystr;
        SqlDataAdapter dadapter;
        DataSet dset;
        PagedDataSource adsource;
        string connstring = "Data Source = USER-PC\\SQLEXPRESS; Initial Catalog = medicalvoyager; Integrated Security = true";
        int pos;
        SqlConnection con = new SqlConnection("Data Source = USER-PC\\SQLEXPRESS; Initial Catalog = medicalvoyager; Integrated Security = true");
        protected void Page_Load(object sender, EventArgs e)
        {
            querystr = Request.QueryString["Question"];
            call1();
            if (!IsPostBack)
            {
                ViewState["vs"] = pos;
            }
           pos = (int)this.ViewState["vs"];
            databind();
           

        }
        public void databind()
        {
            try
            {
                if (querystr.ToString() == "")
                {
                    Response.Redirect("Forum.aspx");
                }
                else
                {
                    dadapter = new SqlDataAdapter("select * from forum_answers where Questionid = @questionid", connstring);
                    dadapter.SelectCommand.Parameters.AddWithValue("@questionid", querystr);
                    dset = new DataSet();
                    adsource = new PagedDataSource();
                    dadapter.Fill(dset);
                    adsource.DataSource = dset.Tables[0].DefaultView;
                    adsource.PageSize = 7;
                    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();
                }
            }
       
        catch(Exception ex)
         {
             Response.Write(ex.ToString());
       Response.Redirect("Error_Page.aspx");

         }

        }
 
Share this answer
 
Try the following Code



ASP.NET
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="5" EnableViewState="true" Width="88%">
  <itemtemplate>Xyz </itemtemplate>
    

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:ConnString %>'SelectCommand="SELECT x,y,z ...">
         
                    <tr>
                        <td>
                              <asp:Label ID="lblCurrentPage" runat="server" Visible="true">
                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                              <asp:Button ID="cmdPrev" runat="server" Text=" << " OnClick="cmdPrev_Click">
                            
                             <asp:Button ID="cmdNext" runat="server" Text=" >> " OnClick="cmdNext_Click">
                            
                        </td>
                    </tr>



in the code behind :

C#
 protected void items()
    {
        PagedDataSource objDs = new PagedDataSource();
        DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        objDs.DataSource = dv;
        objDs.AllowPaging = true;
        objDs.PageSize = 20;
        objDs.CurrentPageIndex = CurrentPage;
        lblCurrentPage.Text = "Page:" + (CurrentPage + 1).ToString() + " Of " + objDs.PageCount.ToString();
        cmdPrev.Enabled = !objDs.IsFirstPage;
        cmdNext.Enabled = !objDs.IsLastPage;
        DataList1.DataSource = objDs;
       DataList1.DataBind();
 
    }
 

protected void cmdPrev_Click(object sender, EventArgs e)
    {
        try
        {
            CurrentPage -= 1;
            items();
        }
        catch (Exception ex)
        {
            Logger.LogException(ex);
        }
    }
 
    protected void cmdNext_Click(object sender, EventArgs e)
    {
        try
        {
 
            CurrentPage += 1;
            items();
        }
        catch (Exception ex)
        {
            Logger.LogException(ex);
        }
    }


[Edit - disabled Treat my content as plain text, not as HTML option]
 
Share this answer
 
v3
Grid view



C#
AllowPaging="True" PageSize="5"  AllowSorting="true"


protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            FillGrid();

        }
 
Share this answer
 
Comments
Ashika s 26-Jun-12 7:38am    
question is for datalist not gridview. please read the question and answer?
Go through below link

http://forums.asp.net/t/1108198.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900