Use Of PagedDataSource Class


Hello Friends Today I wil share with you new Experience which i did in my company that’s Use of Paged Data Source Class.
It’s Really a cool class and give us smart of to paging.
With Just A Smart Coding.

for This You will have to no need to take tension of Cursors ,Difficult logic’s
Simply Make a new Web Site
and
Do Following Steps of Coding as Given below.
Step 1:Make Presentation view of your page as below given
make a new web forma and place on page
1)DropDownlist(with soem entries)
2) DataList 1(with a link button in itemtemplate)
3) DataList 2(with some bind records using eval)
4) Two Link Button (previous and next)

<p>

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table>
<tr>
<td>
<asp:DropDownList ID=”ddlselect” runat=”server” AutoPostBack=”True”
onselectedindexchanged=”ddlselect_SelectedIndexChanged”>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
</asp:DropDownList>
<asp:LinkButton ID=”LinkButton1″ Text=”Previous” runat=”server”
onclick=”LinkButton1_Click”></asp:LinkButton>
<asp:DataList ID=”DataList2″ runat=”server” RepeatDirection=”Horizontal”
onitemcommand=”DataList2_ItemCommand”
onitemdatabound=”DataList2_ItemDataBound”
onselectedindexchanged=”DataList2_SelectedIndexChanged”>
<ItemTemplate>
<asp:LinkButton ID=”lnkbtnPaging” runat=”server” CommandArgument='<%# Eval(“PageIndex”) %>’
CommandName=”lnkbtnPaging” Text='<%# Eval(“PageText”) %>’></asp:LinkButton>
</ItemTemplate>
</asp:DataList><br />
<asp:LinkButton ID=”lnkNext”  Text=”Next” runat=”server”
onclick=”lnkNext_Click”></asp:LinkButton>
</td>
</tr>
</table>
<asp:DataList ID=”DataList1″ runat=”server”>
<ItemTemplate>
<asp:Label ID=”Label1″ runat=”server” Text='<%# Eval(“BookId”) %>’></asp:Label>
<asp:Label ID=”Label2″ runat=”server” Text='<%# Eval(“BookName”) %>’></asp:Label>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
</p>

Step 2: Now Do coding As Given Below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{

PagedDataSource pds = new PagedDataSource();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
string sql = “Select * from TbBook Order By BookId”;
SqlDataAdapter da = new SqlDataAdapter(sql, ConfigurationManager.ConnectionStrings[“Or”].ConnectionString);
DataTable dt = new DataTable();
da.Fill(dt);
pds.DataSource = dt.DefaultView;
pds.AllowPaging = true;
pds.PageSize = Convert.ToInt16(ddlselect.SelectedValue);
pds.CurrentPageIndex = CurrentPage;
lnkNext.Enabled = !pds.IsLastPage;
LinkButton1.Enabled = !pds.IsFirstPage;
DataList1.DataSource = pds;
DataList1.DataBind();
doPaging();
}
public int CurrentPage
{

get
{
if (this.ViewState[“CurrentPage”] == null)
return 0;
else
return Convert.ToInt32(this.ViewState[“CurrentPage”].ToString());
}
set
{
this.ViewState[“CurrentPage”] = value;
}

}

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);
}
DataList2.DataSource = dt;
DataList2.DataBind();
}

protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals(“lnkbtnPaging”))
{
CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
BindGrid();
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
BindGrid();
}
protected void lnkNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
BindGrid();
}
protected void DataList2_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void ddlselect_SelectedIndexChanged(object sender, EventArgs e)
{
CurrentPage = 0;
BindGrid();
}
protected void DataList2_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;
}
}
}

Note :Never Forget to make table of tbbook with columns and fill dummy entries int that.
Then Press F5 and Run your web site see the handsome paging of your site.
enjoy and do programming.

Advertisements