Tuesday, 27 May 2014

How to use ListView and DataPager in asp.net or Using DataPager control in asp.net








Description:-

In this example we explain that how to use DataPager control with Listview in Asp.Net

What is DataPager

DataPager Provides paging functionality for data-bound controls that implement the IPageableItemContainer interface, such as the ListView control.

Why use DataPager

The ListView is a hybrid control between a DataGrid control and Repeater control that join or combines the templating of the Repeater control and the editing features of the data grid control.

We all know that The ListView control doesn't support paging, so the DataPager serves as an external control to provide paging facility.
 DataPager control can be associated with the data-bound control by using the PagedControlID property. Alternatively, by putting the DataPager control inside the data-bound control hierarchy. For example, in the ListView control, DataPager control can be kept inside the ListView-LayoutTemplate.
The number of rows or items that are displayed for each page of data can be customized by changing the PageSize property of the Datapager control.
Pager Fields
Here is some navigation property of the  DataPager control to display navigation controls.
Pager field type
Description
NextPreviousPagerField
Enables users to navigate through pages one page at a time, or to jump to the first or last page.
NumericPagerField
Enables users to select a page by page number.
TemplatePagerField
Enables you to create a custom paging UI.

Page Properties
The following table property escribes the number of rows are displayed in a control.
Property
Description
MaximumRows
The maximum number of records that are displayed for each page of data.
StartRowIndex
The index of the first record that is displayed on a page of data.
TotalRowCount
The total number of records that are available in the underlying data source.

How to use DataPager in Asp.Net

In order to use the DataPager control with the ListView we need to do two things
1. The ID of the ListView control is set for the PagedControlID property of the DataPager control.
2.  and second is you need to define the event OnPagePropertiesChanging which will be raised or fired when the DataPager Page is changed or clicked.

You will also need to import the following namespaces.

using System.Data;
using System.Configuration;
using System.Data.SqlClient;



Handling Paging in ListView using DataPager control
When the page is changed or clicked inside the DataPager the following event hander of the ListView is fired or triggered.
Here first the DataPager control is found and then its SetPageProperties method is executed so that the page numbers are recreated.

protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        (lvemp.FindControl("dtpaging") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
        this.BindListView();
    }


Sorting Row data in gridview Gridview Sorting 

How to handle Concurrency in Linq to Sql Concurrency Example


Cs.aspx:-


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>

<!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>Using DataPager control with example in ASP.Net</title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 12pt;
        }
        table
        {
            border: 1px solid #ccc;
        }
        table th
        {
            background-color: #F8F8F8;
            color: #333;
            font-weight: bold;
        }
        table th, table td
        {
            padding: 5px;
            border-color: #fff;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ListView ID="lvemp" runat="server" GroupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1"
        OnPagePropertiesChanging="OnPagePropertiesChanging">
        <LayoutTemplate>
            <table cellpadding="0" cellspacing="0">
                <tr>
                    <th>
                        Name
                    </th>
                    <th>
                        Mo NO
                    </th>
                    <th>
                        Country
                    </th>
                </tr>
                <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
                <tr>
                    <td colspan="3">
                        <asp:DataPager ID="dtpaging" runat="server" PagedControlID="lvemp" PageSize="3">
                            <Fields>
                                <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
                                    ShowNextPageButton="false" />
                                <asp:NumericPagerField ButtonType="Link" />
                                <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false"
                                    ShowPreviousPageButton="false" />
                            </Fields>
                        </asp:DataPager>
                    </td>
                </tr>
            </table>
        </LayoutTemplate>
        <GroupTemplate>
            <tr>
                <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
            </tr>
        </GroupTemplate>
        <ItemTemplate>
            <td>
                <%# Eval("Name") %>
            </td>
            <td>
                <%# Eval("Mo No") %>
            </td>
            <td>
                <%# Eval("Country") %>
            </td>
        </ItemTemplate>
    </asp:ListView>
    </form>
</body>
</html>



Cs.aspx.cs:-

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.Configuration;

public partial class CS : System.Web.UI.Page
{
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!this.IsPostBack)
        {

            this.BindListView();
        }
    }

    private void BindListView()
    {
        if (dt == null)
        {
            dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Mo No");
            dt.Columns.Add("Country");
            DataRow dr1 = dt.NewRow();
            dr1[0] = "Kirit";
            dr1[1] = "9978718797";
            dr1[2] = "India";
            dt.Rows.Add(dr1);
            DataRow dr2 = dt.NewRow();
            dr2[0] = "Amit";
            dr2[1] = "652718797";
            dr2[2] = "India";
            dt.Rows.Add(dr2);
            DataRow dr3 = dt.NewRow();
            dr3[0] = "Rahul";
            dr3[1] = "9429168522";
            dr3[2] = "Australia";
            dt.Rows.Add(dr3);
            DataRow dr4 = dt.NewRow();
            dr4[0] = "Ajay";
            dr4[1] = "9978718797";
            dr4[2] = "Pakistan";
            dt.Rows.Add(dr4);
            DataRow dr5 = dt.NewRow();
            dr5[0] = "Ketan";
            dr5[1] = "9971284522";
            dr5[2] = "India";
            dt.Rows.Add(dr5);

            lvemp.DataSource = dt;
            lvemp.DataBind();
        }
    }

    protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        (lvemp.FindControl("dtpaging") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
        this.BindListView();
    }
}



0 comments:

Post a Comment