Custom Paging In Datalist Control
CustomPaging.aspx
CustomPaging.aspx.cs
CustomPaging.aspx
<%@ 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"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="dtlEmp" runat="server"> <HeaderTemplate> <table> <tr> <th> ID </th> <th> Name </th> <th> DepID </th> <th> Salary </th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td>
<asp:Label ID="lblID" runat="server"
Text='<%# Bind("id") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</td>
<td><asp:Label ID="lblDepID" runat="server" Text='<%# Bind("depid") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblSalary" runat="server" Text='<%# Bind("Salary") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
<table>
<tr>
<td><a href="Default.aspx#BookMark" id="First" onserverclick="ShowFirstPage"
runat="server"> <img src="firstpage.gif" alt="" /> </a> </td> <td> <a href="Default.aspx#BookMark" id="Prev" onserverclick="ShowPrevPage"
runat="server"> <img src="prevpage.gif" alt="" /> </a> </td> <td> <a href="Default.aspx#BookMark" id="Next" onserverclick="ShowNextPage"
runat="server"> <img src="nextpage.gif" alt="" /> </a> </td> <td>
<a href="Default.aspx#BookMark" id="Last" onserverclick="ShowLastPage"
runat="server"> <img src="lastpage.gif" alt="" /> </a> </td> </tr> </table> <asp:Label ID="lblCurrentIndex" runat="server" Text="0"></asp:Label> <asp:Label ID="lblPagesize" runat="server" Text="5"></asp:Label> <asp:Label ID="lblRecordCount" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
CustomPaging.aspx.cs
using System;
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;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection objcn;
DataSet objDs;
SqlDataAdapter objAd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindData();
}
public void BindData()
{
string Query="select * from tblEmp";
objcn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
objAd = new SqlDataAdapter(Query, objcn);
objDs = new DataSet();
if (!IsPostBack)
{
objAd.Fill(objDs);
lblRecordCount.Text=(objDs.Tables[0].Rows.Count).ToString();
//objDs=null;
//objDs=new DataSet();
}
objAd.Fill(objDs,Convert.ToInt32(lblCurrentIndex.Text),Convert.ToInt32(lblPagesize.Text),"tblEmp");
dtlEmp.DataSource = objDs.Tables["tblEmp"].DefaultView;
dtlEmp.DataBind();
}
public void ShowFirstPage(object sender, EventArgs e)
{
lblCurrentIndex.Text = "0";
BindData();
}
public void ShowPrevPage(object sender, EventArgs e)
{
lblCurrentIndex.Text=((Convert.ToInt32(lblCurrentIndex.Text))-Convert.ToInt32(lblPagesize.Text)).ToString();
if(Convert.ToInt32(lblCurrentIndex.Text)<0)
{
lblCurrentIndex.Text="0";
}
BindData();
}
public void ShowNextPage(object sender, EventArgs e)
{
if (Convert.ToInt32(lblCurrentIndex.Text) + Convert.ToInt32(lblPagesize.Text) < Convert.ToInt32(lblRecordCount.Text))
{
lblCurrentIndex.Text = (Convert.ToInt32(lblCurrentIndex.Text)+ Convert.ToInt32(lblPagesize.Text)).ToString();
}
BindData();
}
public void ShowLastPage(object sender, EventArgs e)
{
int Mod = Convert.ToInt32(lblRecordCount.Text) /Convert.ToInt32(lblPagesize.Text);
if (Mod > 0)
{
lblCurrentIndex.Text = (Convert.ToInt32(lblRecordCount.Text) -Mod).ToString();
}
else
{
lblCurrentIndex.Text=(Convert.ToInt32(lblRecordCount.Text)-Convert.ToInt32(lblPagesize.Text)).ToString(); } BindData(); } }
Thanks, it is very useful article.
ReplyDeletecan I share it in my blog or not.
you can link this post on you blog.
ReplyDelete