Thursday, 10 October 2013

How to implement paging in DataList in ASP.NET

While mistreatment the gridView and listView it's a better pratice to use the information electronic device management for pagging purpose.

But whereas you're operating with datalist management then you may face the important drawback, as a result of information|the info|the information}list doesn't support the data electronic device.

In order to impliment the paging functionalty in information list here is that the easiest way.

Take 2 linkbuttons below the information list, named Previous and Next as below.

<asp:LinkButtonID="lbtnPrevious"runat="server"Text= "Previous"OnClick="lbtnPrevious_onClick"></asp:LinkButton>
<asp:LinkButtonID="lbtnNext" runat="server" Text="Next"OnClick="lbtnNext_onClick"></asp:LinkButton>

Now in the code behind declare the
" ViewState["currentPage"] = 0; " in page load event.
Then do as I did in the BindDataList() method.
   ViewState["currentPage"] = 0;
     protected void BindDataList()
         {
             DataTable dt = newDataTable();
             dt = null // assign the datatable with what you want to assign.  
             PagedDataSource page = newPagedDataSource();
             page.AllowPaging = true;
             page.DataSource = dt.DefaultView;
             page.PageSize = 6;
             page.CurrentPageIndex =Convert.ToInt32(ViewState["currentPage"]);
             dlLogoImages.DataSource = page;
             dlLogoImages.DataBind();
             lbtnNext.Enabled = !page.IsLastPage; // Disable the next button if the page is the last page.
             lbtnPrevious.Enabled = !page.IsFirstPage; // Disable the Previuos button if the page is the first page.
         }


 protectedvoid lbtnPrevious_onClick(object sender, EventArgs e)
        {   
            int currentPage = Convert.ToInt32(ViewState["currentPage"]);
            currentPage -= 1; 
            ViewState["currentPage"] = currentPage;
            BindDataList();
        }

   protectedvoid lbtnNext_onClick(object sender, EventArgs e)
       {
            int currentPage = Convert.ToInt32(ViewState["currentPage"]);
            currentPage += 1; 
            ViewState["currentPage"] = currentPage;
            BindDataList();   
      }

Now paging is done in DataList.

    protectedvoid lbtnPrevious_onClick(object sender, EventArgs e)
        {   
            int currentPage = Convert.ToInt32(ViewState["currentPage"]);
            currentPage -= 1; 
            ViewState["currentPage"] = currentPage;
            BindDataList();
        }

   protectedvoid lbtnNext_onClick(object sender, EventArgs e)
       {
            int currentPage = Convert.ToInt32(ViewState["currentPage"]);
            currentPage += 1; 
            ViewState["currentPage"] = currentPage;
            BindDataList();   
      }



I hope this post will be helpful for you

0 comments:

Post a Comment