DataList Paging With PagedDataSource
Description:
DataList.aspx:
DataList.aspx.cs
Paging can be done in DataList Using PagedDataSource.
Here I am using 2 datalists one for displaying the content(Data) and another datalist for paging.
Inside the paging datalist I am using link button for showing the paging links.
DataList.aspx:
Write the Below code as stated.
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="DataList.aspx.cs" Inherits="DataList" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:DataList ID="dlist" runat="server" RepeatColumns="3" DataKeyField="ImageID">
<ItemTemplate>
<img id="ImageURLLabel" alt="Image" runat="server" style="height: 45px; width: 20%"
src='<%# Eval("ImageURL") %>' />
<br />
<asp:Label ID="ImageDescLabel" runat="server" Text='<%# Eval("ImageDesc") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:DataList ID="dlPaging" runat="server" RepeatDirection="Horizontal" OnItemCommand="dlPaging_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnPaging" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
CommandName="lnkbtnPaging" Text='<%# Eval("PageText") %>'></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</asp:Content>
PageSize is equal to 12 signifies it will display only 12 images in the first page of datalist
Copy the below in code behind.
public partial class DataList : System.Web.UI.Page
{
PagedDataSource pds = new PagedDataSource();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//bindData to Datalist
BindGrid();
}
}
//Maintaining the current page index in session
public int CurrentPage
{
get
{
if (this.ViewState["CurrentPage"] == null)
return 0;
else
return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
}
set
{
this.ViewState["CurrentPage"] = value;
}
}
//adding paging
private void doPaging()
{
DataTable dt = new DataTable();
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageText");
for (int i = 0; i < pds.PageCount; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i + 1;
dt.Rows.Add(dr);
}
dlPaging.DataSource = dt;
dlPaging.DataBind();
}
//Bind Data with paging
private void BindGrid()
{
string sql = "select * from Image";
SqlDataAdapter da = new SqlDataAdapter(sql, "Data Source=TAPA-PC;Initial Catalog=Customer;User ID=sa;Password=123");
DataTable dt = new DataTable();
da.Fill(dt);
pds.DataSource = dt.DefaultView;
pds.AllowPaging = true;
pds.PageSize = 12;//Convert.ToInt16(ddlPageSize.SelectedValue);
pds.CurrentPageIndex = CurrentPage;
//lnkbtnNext.Enabled = !pds.IsLastPage;
//lnkbtnPrevious.Enabled = !pds.IsFirstPage;
dlist.DataSource = pds;
dlist.DataBind();
doPaging();
}
//on click of linkbutton this event will fire to bind the corresponding data.
protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("lnkbtnPaging"))
{
CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
BindGrid();
}
}
//Previous Link
protected void lnkbtnPrevious_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
BindGrid();
}
//next Link
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
BindGrid();
}
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
CurrentPage = 0;
BindGrid();
}
protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
{
LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
{
lnkbtnPage.Enabled = false;
lnkbtnPage.Font.Bold = true;
}
}
}
Subscribe to:
Post Comments (Atom)
Labels
- Abstraction in Object Oriented Programming (OOPS) Concept (1)
- Access ChildControls in Gridview using Javascript (1)
- Add a WCF Service Reference to the Client (1)
- ASP.Net GridView Highlight Row onmouseover (1)
- ASP.NET View State And ViewStateEncryptionModes Overview (1)
- Calling Javascript From Any Part Of Code Behind Page By Registering The Script (1)
- Check And UnCheck CheckBoxes In Gridview using javascript (1)
- contact your server administrator. (1)
- DataControlField class (1)
- DataKeys ID (Identity Column) in Child Controls events in GridView (1)
- DataList Paging With PagedDataSource (1)
- Declaring Session in Asp.Net (1)
- Difference between Struct and Class (1)
- Displaying Images In GridView From DataBase (1)
- Dynamic Sitemaps in ASP.NET (1)
- Err: You must install Office SharePoint Server 2007 – Please read Microsoft Knowledge Base article: 962935 with the most recent service pack (1)
- GridView Class (1)
- Gridview Inside GridView (1)
- Gridview Paging using C# (1)
- Gridview Sorting Using C# (1)
- GridView.RowDataBound EventEvent (1)
- GridViewRow (1)
- Inserting Images To Database And Display in GridView (1)
- Microsoft Office Sharepoint Server 2007 on Windows Server 2008 – This Program is blocked due to compatibility issues (1)
- ModalPopUp Using CSS and Div To Reduce the Weight On WebPage (1)
- Session State in Asp.Net OverView (1)
- SharePoint 2010 – The form cannot be rendered. This may be due to a misconfiguration of the Microsoft SharePoint Server State Service. For more information (1)
- Tooltip GridView Header (1)
- Tree View Menu Control (1)
- WCF Service (12)
- What is Encapsulation in OOPS? (1)
Topics
- Abstraction in Object Oriented Programming (OOPS) Concept (1)
- Access ChildControls in Gridview using Javascript (1)
- Add a WCF Service Reference to the Client (1)
- ASP.Net GridView Highlight Row onmouseover (1)
- ASP.NET View State And ViewStateEncryptionModes Overview (1)
- Calling Javascript From Any Part Of Code Behind Page By Registering The Script (1)
- Check And UnCheck CheckBoxes In Gridview using javascript (1)
- contact your server administrator. (1)
- DataControlField class (1)
- DataKeys ID (Identity Column) in Child Controls events in GridView (1)
- DataList Paging With PagedDataSource (1)
- Declaring Session in Asp.Net (1)
- Difference between Struct and Class (1)
- Displaying Images In GridView From DataBase (1)
- Dynamic Sitemaps in ASP.NET (1)
- Err: You must install Office SharePoint Server 2007 – Please read Microsoft Knowledge Base article: 962935 with the most recent service pack (1)
- GridView Class (1)
- Gridview Inside GridView (1)
- Gridview Paging using C# (1)
- Gridview Sorting Using C# (1)
- GridView.RowDataBound EventEvent (1)
- GridViewRow (1)
- Inserting Images To Database And Display in GridView (1)
- Microsoft Office Sharepoint Server 2007 on Windows Server 2008 – This Program is blocked due to compatibility issues (1)
- ModalPopUp Using CSS and Div To Reduce the Weight On WebPage (1)
- Session State in Asp.Net OverView (1)
- SharePoint 2010 – The form cannot be rendered. This may be due to a misconfiguration of the Microsoft SharePoint Server State Service. For more information (1)
- Tooltip GridView Header (1)
- Tree View Menu Control (1)
- WCF Service (12)
- What is Encapsulation in OOPS? (1)
0 Responses to “DataList Paging With PagedDataSource”
Post a Comment