DataList List Paging in ASP.Net using C#
DataListPaging.aspx
<asp:DataList ID="dlCities" runat="server" Width="250px" RepeatColumns="1" RepeatDirection="Vertical" BorderColor="Maroon" BorderStyle="Solid" BorderWidth="1" OnItemCommand="dlCities_ItemCommand"> <HeaderStyle VerticalAlign="Middle" HorizontalAlign="Center" /> <HeaderTemplate> <div style="width: 250px; height: 25px; background-color:Maroon;color:White;"> Cities of India </div> </HeaderTemplate> <ItemStyle HorizontalAlign="Center" /> <ItemTemplate> <asp:Label ID="lblCityName" runat="server" Text='<%# Bind("CityName") %>'></asp:Label> </ItemTemplate> <SeparatorTemplate> <div style="width: 250px; height: 1px; background-color:Maroon;"> </div> </SeparatorTemplate> <FooterStyle VerticalAlign="Middle" HorizontalAlign="Center" /> <FooterTemplate> <div style="width: 250px; height: 25px; background-color:Maroon;"> <asp:LinkButton ID="lbFirst" runat="server" Text="|<" CommandName="First" ForeColor="White" ToolTip="First"></asp:LinkButton> <asp:LinkButton ID="lbPrevious" runat="server" Text="<" CommandName="Previous" ForeColor="White" ToolTip="Previous"></asp:LinkButton> <asp:LinkButton ID="lbNext" runat="server" Text=">" CommandName="Next" ForeColor="White" ToolTip="Next"></asp:LinkButton> <asp:LinkButton ID="lbLast" runat="server" Text=">|" CommandName="Last" ForeColor="White" ToolTip="Last"></asp:LinkButton> </div> </FooterTemplate> </asp:DataList>
DataListPaging.aspx.cs
//A Class City with two Properties. public class City { private int _CityID; private string _CityName; public int CityID { get { return _CityID; } set { _CityID = value; } } public string CityName { get { return _CityName; } set { _CityName = value; } } }
//Some variables to maintain Page status. static int StartIndex = 0; const int PageSize = 5; static int TotalPages = 0;
//This method returns 5 records for binding based on the Start index. List<City> GetAllCities(int _StartIndex, int _PageSize, out int TotalPages) { List<City> CityList = new List<City>() { new City{ CityID = 1,CityName="New Delhi"}, new City{ CityID = 2,CityName="Mumbai"}, new City{ CityID = 3,CityName="Kolkatta"}, new City{ CityID = 4,CityName="Chennai"}, new City{ CityID =5,CityName="Bangalore"}, new City{ CityID =6,CityName="Hyderabad"}, new City{ CityID=7,CityName="Pune"}, new City{ CityID=8,CityName="Noida"}, new City{ CityID=9,CityName="Ahmedabad"}, new City{ CityID=10,CityName="Baroda"}, new City{ CityID=11,CityName="Nagpur"}, new City{ CityID=12,CityName="Jaipur"}, new City{ CityID=13,CityName="Chandigarh"}, new City{ CityID=14,CityName="Amristar"}, new City{ CityID=15,CityName="Madurai"}, new City{ CityID=16,CityName="Coimbatore"}, new City{ CityID=17,CityName="Trichy"}}; int Total = CityList.Count(); TotalPages = (Total % PageSize) > 0 ? (Total / PageSize) : (Total / PageSize) - 1; return CityList.Skip(_StartIndex * _PageSize).Take(_PageSize).ToList(); }
//Binds the Data to the DataList void BindAllCities() { List<City> CityList = GetAllCities(0, PageSize, out TotalPages); if (CityList.Count > 0) dlCities.DataSource = CityList; else { dlCities.DataSource = null; dlCities.ShowFooter = false; dlCities.ShowHeader = false; } dlCities.DataBind(); ManagePaging(); }
//Binding the data on Page Load protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { StartIndex = TotalPages = 0; BindAllCities(); } }
//Based on the Paging Command... DataList is binded... protected void dlCities_ItemCommand(object sender, DataListCommandEventArgs e) { if (e.CommandName == "First") StartIndex = 0; else if (e.CommandName == "Next") StartIndex++; else if (e.CommandName == "Previous") StartIndex--; else if (e.CommandName == "Last") StartIndex = TotalPages; dlCities.DataSource = GetAllCities(StartIndex, PageSize, out TotalPages); dlCities.DataBind(); ManagePaging(); }
//Method to manage the Paging process on every paging command void ManagePaging() { LinkButton lbFirst = dlCities.Controls[dlCities.Controls.Count - 1].FindControl("lbFirst") as LinkButton; LinkButton lbPrevious = dlCities.Controls[dlCities.Controls.Count - 1].FindControl("lbPrevious") as LinkButton; LinkButton lbNext = dlCities.Controls[dlCities.Controls.Count - 1].FindControl("lbNext") as LinkButton; LinkButton lbLast = dlCities.Controls[dlCities.Controls.Count - 1].FindControl("lbLast") as LinkButton; if (StartIndex == 0 && StartIndex < TotalPages) { lbFirst.Enabled = lbPrevious.Enabled = false; lbNext.Enabled = lbLast.Enabled = true; } else if (StartIndex > 0 && StartIndex < TotalPages) lbFirst.Enabled = lbPrevious.Enabled = lbNext.Enabled = lbLast.Enabled = true; else if (StartIndex == TotalPages && StartIndex > 0) { lbFirst.Enabled = lbPrevious.Enabled = true; lbNext.Enabled = lbLast.Enabled = false; } else if (StartIndex == TotalPages) lbFirst.Enabled = lbPrevious.Enabled = lbNext.Enabled = lbLast.Enabled = false; }
1 comment:
super da mani
Post a Comment