.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

DataList Paging in ASP.NEt

Posted By:Athira Sreejith       Posted Date: March 24, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

Design:

<form id="Form1" method="post" runat="server">

<h2 align="center"><font face="verdana">Paging in DataListfont>h2>

<a name="this">a>

<asp:DataList ID="dList" Runat="server" Width="100%"

ItemStyle-BackColor="Beige"

ItemStyle-Font-Name="Verdana"

BorderWidth="1"

HeaderStyle-Font-Name="Verdana"

EnableViewState="False">

<HeaderTemplate>

<table width="100%" style="font: 10pt verdana" cellpadding=0 cellspacing=0>

<tr style="background-color:FF0000">

<th align=left><font color="#FFFFFF">Book IDfont>th>

<th align=left><font color="#FFFFFF">Author IDfont>th>

<th align=left><font color="#FFFFFF">Book Namefont>th>

<th align=left><font color="#FFFFFF">Pricefont>th>

tr>

HeaderTemplate>

<ItemTemplate>

<tr style="background-color:#f5f5dc">

<td><%# DataBinder.Eval(Container.DataItem, "BookID") %>td>

<td><%# DataBinder.Eval(Container.DataItem, "AuthorID") %>td>

<td><%# DataBinder.Eval(Container.DataItem, "BookName") %>td>

<td><%# DataBinder.Eval(Container.DataItem, "Price") %>td>

tr>

ItemTemplate>

<FooterTemplate>

table>

FooterTemplate>

asp:DataList>

<table width=100% align="right">

<tr>

<td width=76% align=left>

<asp:label ID="lblStatus"

Runat="server"

Font-Name="verdana"

Font-Size="10pt" />

td>

<td width=6%>

<asp:Button runat="server" ID="FirstPage" Text="<< First" OnClick="FirstPage_Click" />

td>

<td width=6%>

<asp:Button runat="server" ID="PrevPage" Text="< Prev" OnClick="PrevPage_Click" />

td>

<td width=6%>

<asp:Button runat="server" ID="NextPage" Text="Next >" OnClick="NextPage_Click" />

td>

<td width=6%>

<asp:Button runat="server" ID="LastPage" Text="Last >>" OnClick="LastPage_Click" />

td>

tr>

table>

<br /><br />

<asp:label ID="intCurrIndex"

Visible="False"

Runat="server" />

<asp:label ID="intPageSize"

Visible="False"

Runat="server" />

<asp:label ID="intRecordCount"

Visible="False"

Runat="server" />

form>

 

.cs Page:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class datalistpaging : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if(!IsPostBack)

{

intPageSize.Text = "5";

intCurrIndex.Text = "0";

loaddata();

}

}

public void loaddata()

{

SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);

con.Open();

string qry = "Select * from Book";

SqlDataAdapter ad = new SqlDataAdapter(qry, con);

DataSet ds = new DataSet();

ad.Fill(ds,Convert.ToInt32(intCurrIndex.Text),Convert.ToInt32(intPageSize.Text),"Book");

if (ds.Tables[0].Rows.Count > 0)

{

this.dList.DataSource = ds.Tables[0].DefaultView;

this.dList.DataBind();

printstatus();

}

}

public void printstatus()

{

lblStatus.Text = "Total Records:" + intRecordCount.Text;

lblStatus.Text += "-Showing Page:";

lblStatus.Text+=Convert.ToString(Convert.ToInt32((Convert.ToInt32(intCurrIndex.Text))/(Convert.ToInt32(intPageSize.Text)))+1);

lblStatus.Text+=" of ";

if(((Convert.ToInt32(intRecordCount.Text))%(Convert.ToInt32(intPageSize.Text)))>0)

{

lblStatus.Text+=Convert.ToString(Convert.ToInt32(Convert.ToInt32(intRecordCount.Text)/Convert.ToInt32(intPageSize.Text)+1));

}

else

{

lblStatus.Text+=Convert.ToString((Convert.ToInt32(intRecordCount.Text))/(Convert.ToInt32(intPageSize.Text)));

}

lblStatus.Text+="";

}

protected void FirstPage_Click(object sender, EventArgs e)

{

intCurrIndex.Text = "0";

loaddata();

}

protected void PrevPage_Click(object sender, EventArgs e)

{

intCurrIndex.Text = Convert.ToString((Convert.ToInt32(intCurrIndex.Text)) - (Convert.ToInt32(intPageSize.Text)));

if ((Convert.ToInt32(intCurrIndex.Text)) < 0)

{

intCurrIndex.Text = "0";

}

loaddata();

}

protected void NextPage_Click(object sender, EventArgs e)

{

int ind1 = Convert.ToInt32(intCurrIndex.Text);

int ind2 = Convert.ToInt32(intRecordCount.Text);

if ((ind1 + 1) < ind2)

{

intCurrIndex.Text = Convert.ToString((Convert.ToInt32(intCurrIndex.Text)) + (Convert.ToInt32(intPageSize.Text)));

}

loaddata();

}

protected void LastPage_Click(object sender, EventArgs e)

{

int tint;

tint = ((Convert.ToInt32(intRecordCount.Text)) % (Convert.ToInt32(intPageSize.Text)));

if (tint > 0)

{

intCurrIndex.Text = Convert.ToString(Convert.ToInt32(intRecordCount.Text) - tint);

}

else

{

intCurrIndex.Text = Convert.ToString(Convert.ToInt32(intRecordCount.Text) - Convert.ToInt32(intPageSize.Text));

}

loaddata();

}

}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend