Jan 20, 2014

How to use DataPager control with Data List View in asp.net c#

UI:



HTML : (.aspx)
<!--Listview with pagger-->
                <asp:ListView runat="server" ID="ListView1" GroupItemCount="3" OnPreRender="ListView1_PreRender1">
                    <LayoutTemplate>
                        <table cellpadding="2" runat="server" id="tblProducts" style="height: 320px;">
                            <tr align="left" runat="server" id="groupPlaceholder">
                            </tr>
                        </table>
                        <asp:DataPager runat="server" ID="dpMyDatePager" PageSize="9">
                            <Fields>
                                <asp:NumericPagerField ButtonCount="2" PreviousPageText="<--" NextPageText="-->" />
                                <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowFirstPageButton="True" />
                            </Fields>
                        </asp:DataPager>
                    </LayoutTemplate>
                    <GroupTemplate>
                        <tr runat="server" id="productRow" style="height: 80px">
                            <td runat="server" id="itemPlaceholder">
                            </td>
                        </tr>
                    </GroupTemplate>
                    <ItemTemplate>
                        <td id="Td1" valign="top" width="279px" align="left" runat="server">
                            <div style="height: 350px; width: 100%; overflow-y: auto; overflow-x: hidden; margin-left:39px;">
                                <table class="mtbl">
                                    <tr>
                                        <td class="rTd">
                                                <img src="<%#Eval("Images")%>" style="width:196px;height:196px;" />
                                        </td>
                                    </tr>
                                </table>
                                <h5>
                                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("Title")%>'></asp:Label>
                                </h5>
                                <div id="details" class="details">
                                    <asp:Label ID="Label2" runat="server" Text='<%#Eval("Description")%>'></asp:Label>
                                </div>
                            </div>
                        </td>
                    </ItemTemplate>
                </asp:ListView>
C# code: (.aspx.cs)
    protected void ListView1_PreRender1(object sender, EventArgs e)
    {
        loadgalary();
    }
    private void loadgalary()
    {
        DataTable dt = ClassGallery.FetchGallery();
        if (dt != null && dt.Rows.Count > 0)
        {
            ListView1.DataSource = dt;
            ListView1.DataBind();
        }
    }


Namespace:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;