Friday, January 30, 2015

How to use paging in DataList

In this article I am going to explain about how to implement paging in DataList control.

DataList displays multiple data itemps where you can supply templates for each item to display data fields in any way to choose. As with GridView, you can select, sort and edit data item.

There are the following template in DataList control, which is given below:

  • Item Template

    It is used for list items.

  • HeaderTemplate

    It is used for output before items.

  • FooterTemplate

    It is used for output after items.

  • Separator Template

    It is used between items in list.

  • AlternatingItemTemplate

    It is used for alternate items.

  • SelectedItemTemplete

    It is used for selected items in the list.
     
  • EditItem Templete

    It is used for items being inserted.
In this article I have only used header and item template.

There are following steps, which are used for paging of DataList:

Step1 Firstly design employee Table in DataBase:




Step2 Source coding of DataList control


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table border="1">
        <asp:DataList ID="DataList1" runat="server">
        <HeaderTemplate>
        <font color="red"><b>Employee Details</b></font>
        </HeaderTemplate>
        <ItemTemplate>
        <tr>
        <td><font color="Green"><b>ID</b></font></td>
        <td><font color="Green"><b>Name</b></font></td>
        <td><font color="Green"><b>Salary</b></font></td>
         </tr>
         <tr>
          <td><font color="#ff8000"><b><%#Eval("id")%></b></font></td>
          <td>  <font color="Fuchsia"><b><%#Eval("name")%></b></font></td>
          <td>  <font color="#663300"><b><%#Eval("salary")%></b></font></td>
         </tr>
        </ItemTemplate>      
        </asp:DataList>
       </table>
         <table width="100%" border="0">
      <tr>
         <td>  <asp:label id="lbl1" runat="server" BackColor="Yellow" BorderColor="Yellow"
                 Font-Bold="True" ForeColor="#FF3300"></asp:label></td>
      </tr>
      <tr>
       <td> 
           <asp:button id="btnPrevious" runat="server" text="Previous" Width="60px" onclick="btnPrevious_Click"
              ></asp:button>
             <asp:button id="btnNext" runat="server" text="Next" Width="60px" OnClick="btnNext_Click"
               ></asp:button>
            </td>
      </tr>
   </table>
    </div>
    </form>
</body>
</html>


Step3  Default.aspx page coding


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls.WebParts;
public partial class _Default : System.Web.UI.Page
{
    SqlDataAdapter _Da;
    DataSet _Ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDataList();
        }
    }
    private void bindDataList()
    {
        _Da = new SqlDataAdapter("select * from emp", "Data Source=.;Initial Catalog=employees;User ID=sa;Password=************");
        _Ds = new DataSet();
        _Da.Fill(_Ds);
        PagedDataSource Pds1 = new PagedDataSource();
        Pds1.DataSource = _Ds.Tables[0].DefaultView;
        Pds1.AllowPaging = true;
        Pds1.PageSize = 3;
        Pds1.CurrentPageIndex = CurrentPage;
        lbl1.Text = "Showing Page: " + (CurrentPage + 1).ToString() + " of " + Pds1.PageCount.ToString();
        btnPrevious.Enabled = !Pds1.IsFirstPage;
        btnNext.Enabled = !Pds1.IsLastPage;
        DataList1. DataSource = Pds1;
        DataList1. DataBind();
    }
    public int CurrentPage
    {
        get
        {
            object Curr1 = this.ViewState["CurrentPage"];
            if (Curr1 == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(Curr1);
            }
        }
        set { this.ViewState["CurrentPage"] = value; }
    }
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        bindDataList();
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        bindDataList();
    }
}


Step4 Previous Output Page Image 


Step5 Using the paging then click on Next button



No comments:

Post a Comment

Kashmir 370 and 35A : The Wound of india

क्या है जम्मू-कश्मीर में लागू धारा 370,35A पूर्ण विवरण Know about 370 act in Jammu एक बार फिर से राजनीति गलियारे में धारा 370,35A को ल...