add paging capability in datalist [Resolved]

Posted by Sushant under .NET Certifications on 3/4/2013 | Points: 10 | Views : 2107 | Status : [Member] | Replies : 4
Plz help me to add paging capability in datalist

Sushat


Responses

Posted by: Satyapriyanayak on: 3/5/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Default.aspx code

<%@ 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>
<table border="1">
<asp:DataList ID="dl1" runat="server">

<HeaderTemplate>
<font color="red"><b>Employee Details</b></font>

</HeaderTemplate>
<itemtemplate>
<tr>
<td><font color="Green"><b>ID</b></font></td>
<td><font color="Green"><b>NAME</b></font></td>
<td><font color="Green"><b>SALARY</b></font></td>
<td><font color="Green"><b>ADDRESS</b></font></td>
<td><font color="Green"><b>DESIGNATION</b></font></td>
</tr>
<tr>
<td> <font color="#ff8000"><b><%#Eval("empid")%></b></font></td>
<td> <font color="Fuchsia"><b><%#Eval("empname")%></b></font></td>
<td> <font color="#663300"><b><%#Eval("empsal")%></b></font></td>
<td> <font color="Purple"><b><%#Eval("empadd")%></b></font></td>
<td> <font color="#808040"><b><%#Eval("empdes")%></b></font></td>
</tr>
</itemtemplate>

</asp:DataList>
</table>
<table width="100%" border="0">
<tr>
<td> <asp:label id="lbl1" runat="server" BackColor="Yellow" BorderColor="Yellow"
Font-Bold="True" ForeColor="#FF3300"></asp:label></td>
</tr>
<tr>
<td> <asp:button id="btnPrevious" runat="server" text="Previous" Width="60px"
onclick="btnPrevious_Click"></asp:button>
<asp:button id="btnNext" runat="server" text="Next" Width="60px"
onclick="btnNext_Click"></asp:button></td>
</tr>
</table>
</div>
</form>
</body>
</html>

Default.aspx.cs code

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
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str ;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddatalist();
}
}
private void binddatalist()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
PagedDataSource Pds1 = new PagedDataSource();
Pds1.DataSource = ds.Tables[0].DefaultView;
Pds1.AllowPaging = true;
Pds1.PageSize = 3;
Pds1.CurrentPageIndex = CurrentPage;
lbl1.Text = "Showing Page: " + (CurrentPage + 1).ToString() + " of " + Pds1.PageCount.ToString();
btnPrevious.Enabled = !Pds1.IsFirstPage;
btnNext.Enabled = !Pds1.IsLastPage;
dl1.DataSource = Pds1;
dl1.DataBind();
con.Close();
}
public int CurrentPage
{
get
{
object s1 = this.ViewState["CurrentPage"];
if (s1 == null)
{
return 0;
}
else
{
return Convert.ToInt32(s1);
}
}

set { this.ViewState["CurrentPage"] = value; }
}

protected void btnPrevious_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
binddatalist();

}
protected void btnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
binddatalist();

}
}


If this post helps you mark it as answer
Thanks

Sushant, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 3/5/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

Refer this link:
This may help you:
http://forums.asp.net/t/1780398.aspx/1

Thanks & Regards,
Santhi .V

Sushant, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raj.Trivedi on: 3/5/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hello Sushant,

Please check this link

http://www.codeproject.com/Articles/15567/Custom-Paging-for-DataList

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Sushant, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 3/6/2013 [Member] Bronze | Points: 25

Up
0
Down
http://www.codeproject.com/Questions/262721/Paging-in-datalist-in-asp-net

Thanks
Pavan Kumar
Mark Answer if this fits the need

Sushant, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response