Tuesday, March 31, 2015

ListView Data control With DataPager ASP.NET C#

Introduction:
In this article I will explain how to display/show data in ListView control With DataPager ASP.NET C#

 HTML Markup
Here I am making use of HTML Table Layout for the ListView control. I have placed the HTML Table inside the LayoutTemplate with Table Header and then a GroupPlaceHolder and finally the Footer row with a DataPager control in it.


<asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
<LayoutTemplate>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>
                CustomerId
            </th>
            <th>
                ContactName
            </th>
            <th>
                Country
            </th>
        </tr>
        <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
        <tr>
            <td colspan = "3">
                <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="10">
                    <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("CustomerId") %>
    </td>
    <td>
        <%# Eval("ContactName") %>
    </td>
    <td>
        <%# Eval("Country") %>
    </td>
</ItemTemplate>
</asp:ListView>
 Associating the DataPager with the ListView control
In order to associate 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. We need to specify the event OnPagePropertiesChanging which will be raised when the DataPager Page is changed or clicked.

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindListView();
    }
}

private void BindListView()
{
        DataTable dt = new DataTable();
        dt.Columns.Add("
CustomerId", typeof(Int32));
        dt.Columns.Add("
ContactName", typeof(string));
        dt.Columns.Add("Education", typeof(string));
        dt.Columns.Add("
Country", typeof(string));
        dt.Columns.Add("
Salary", typeof(decimal));
        dt.Rows.Add(31, "Muhammed Asad", "BCIT", "Karachi", 85);
        dt.Rows.Add(32, "Muhammed Ali", "Msc", "Karachi", 812);
        dt.Rows.Add(33, "Faraz Ahmed", "MS", "Lahore", 67);
        dt.Rows.Add(34, "Asma", "MBA", "Hyderabad", 34);
        dt.Rows.Add(36, "Faheem Shah", "MBBS", "Karachi", 74);
        dt.Rows.Add(37, "Asif Raza", "B.Tech", "Hyderabad", 55);
        dt.Rows.Add(38, "Daniel", "CA", "Karachi", 853);
        dt.Rows.Add(31, "Muhammed Asad", "BCIT", "Karachi", 85);
        dt.Rows.Add(32, "Muhammed Ali", "Msc", "Karachi", 812);
        dt.Rows.Add(33, "Faraz Ahmed", "MS", "Lahore", 67);
        dt.Rows.Add(34, "Asma", "MBA", "Hyderabad", 34);
        dt.Rows.Add(36, "Faheem Shah", "MBBS", "Karachi", 74);
        dt.Rows.Add(37, "Asif Raza", "B.Tech", "Hyderabad", 55);
        dt.Rows.Add(38, "Daniel", "CA", "Karachi", 853);
        dt.Rows.Add(31, "Muhammed Asad", "BCIT", "Karachi", 85);
        dt.Rows.Add(32, "Muhammed Ali", "Msc", "Karachi", 812);
        dt.Rows.Add(33, "Faraz Ahmed", "MS", "Lahore", 67);
        dt.Rows.Add(34, "Asma", "MBA", "Hyderabad", 34);
        dt.Rows.Add(36, "Faheem Shah", "MBBS", "Karachi", 74);
        dt.Rows.Add(37, "Asif Raza", "B.Tech", "Hyderabad", 55);
        dt.Rows.Add(38, "Daniel", "CA", "Karachi", 853);
       
lvCustomers.DataSource = dt;
       
lvCustomers.DataBind(); 
                
                }
protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
    (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    this.BindListView();
}
 

No comments:

Post a Comment