Introduction:
In this example iam going to explain how to give paging to Datalist in asp.net.and also explain how to display images in Datalist.
If you want to check all the example in my site please check this link Ajax all extenders examples and also you check my previous exampleSending mail through smtp server using asp.net
First we need to design the table in Database and give the table name "Data".
Column Name | Datatype |
PID | int(set identity property=true) |
FirstName; | nvarchar(50) |
SecondName | nvarchar(50) |
Age | int |
FatherName | nvarchar(50) |
Your Aspx Code should be like this :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="dlDetails"
runat="server"
RepeatColumns="4">
<ItemTemplate>
<table
cellspacing="0"
cellpadding="5">
<tr>
<td>
<img src="Handler.ashx?autoId=<%# Eval("PID") %>" style="width: 150px; height: 150px;" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblCompanyName"
runat="server"
Text='<%#Eval("FirstName") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblCity"
runat="server"
Text='<%#Eval("SecondName") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblState"
runat="server"
Text='<%#Eval("Age ") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server"
Text='<%#Eval("FatherName") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<table style="width: 506px; margin-left: 80px">
<tr>
<td>
<asp:LinkButton ID="lnkbtnPgFirst"
runat="server"
OnClick="lnkbtnPgFirst_Click">First</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lnkbtnPgPrevious"
runat="server"
OnClick="lnkbtnPgPrevious_Click">Previous</asp:LinkButton>
</td>
<td>
<asp:DataList ID="DataListPaging"
runat="server"
RepeatDirection="Horizontal"
OnItemCommand="DataListPaging_ItemCommand"
OnItemDataBound="DataListPaging_ItemDataBound">
<ItemTemplate>
<asp:LinkButton ID="Pagingbtn"
runat="server"
CommandArgument='<%# Eval("PageIndex") %>'
CommandName="Newpage"
Text='<%# Eval("PageText")%>'></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</td>
<td>
<asp:LinkButton ID="lnkbtnPgNext"
runat="server"
OnClick="lnkbtnPgNext_Click">Next</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="lnkbtnPgLast"
runat="server"
OnClick="lnkbtnPgLast_Click">Last</asp:LinkButton>
</td>
</tr>
<tr>
<td
align="center"
height="40"
colspan="5"
style="background-color:
White">
<asp:Label ID="lblpaging"
runat="server"
Text=""></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
|
if you observe above aspx coding two Datalist has been taken ,one Datalist(dlDetails)is for displaying the data and another datalist(DataListPaging) is for to give paging to the first datalist.
In the Datalist you hace to take below Events
ItemCommand
ItemDataBound
Now add the following namespaces in codebehind
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
write the following code in aspx.cs page
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["connectionStr"].ConnectionString);
int firstindex, lastindex;
PagedDataSource pgsource = new PagedDataSource();
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getGridDetails();
}
}
private int
pagingCurrentPage
{
get
{ //Check
view state is null if null then return current index value as "0"
else return specific page viewstate value
if (ViewState["pagingCurrentPage"]
== null)
{
return 0;
}
else
{
return ((int)ViewState["pagingCurrentPage"]);
}
}
set
{
//Set View statevalue when page is changed through Paging
DataList
ViewState["pagingCurrentPage"]
= value;
}
}
private void
DataListPagingMethod()
{
DataTable dt = new
DataTable();
//Add two column into the DataTable "dt"
//First Column store page index default it start from
"0"
//Second Column store page index default it start from
"1"
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageText");
//Assign First Index starts from which number in paging
data list
firstindex = pagingCurrentPage - 5;
//Set Last index value if current page less than 5 then
last index added "5" values to the Current page else it set
"10" for last page number
if (pagingCurrentPage > 5)
{
lastindex = pagingCurrentPage + 5;
}
else
{
lastindex = 10;
}
//Check last page is greater than total page then reduced
it to total no. of page is last index
if (lastindex > Convert.ToInt32(ViewState["totpage"]))
{
lastindex = Convert.ToInt32(ViewState["totpage"]);
firstindex = lastindex - 10;
}
if (firstindex < 0)
{
firstindex = 0;
}
//Now creating page number based on above first and last
page index
for (int i =
firstindex; i < lastindex; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i + 1;
dt.Rows.Add(dr);
}
//Finally bind it page numbers in to the Paging DataList
DataListPaging.DataSource
= dt;
DataListPaging.DataBind();
}
private void
getGridDetails()
{
try
{
string fetchQuery = "select
* from Data";
SqlCommand cmd = new
SqlCommand(fetchQuery, con);
SqlDataAdapter
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables.Count > 0 &&
ds.Tables[0].Rows.Count > 0)
{
dlDetails.DataSource = ds.Tables[0];
dlDetails.DataBind();
}
DataTable dt = ds.Tables[0];
pgsource.DataSource = dt.DefaultView;
pgsource.AllowPaging = true;
pgsource.PageSize = 4;
pgsource.CurrentPageIndex = pagingCurrentPage;
ViewState["totpage"] =
pgsource.PageCount;
//Below line is used to show page number based on selection
like "Page 1 of 20"
lblpaging.Text = "Page " +
(pagingCurrentPage + 1) + " of " +
pgsource.PageCount;
//Enabled true Link button previous when current page is
not equal first page
//Enabled false Link button previous when current page is
first page
lnkbtnPgPrevious.Enabled = !pgsource.IsFirstPage;
//Enabled true Link button Next when current page is not
equal last page
//Enabled false Link button Next when current page is last
page
lnkbtnPgNext.Enabled = !pgsource.IsLastPage;
//Enabled true Link button First when current page is not
equal first page
//Enabled false Link button First when current page is
first page
lnkbtnPgFirst.Enabled = !pgsource.IsFirstPage;
//Enabled true Link button Last when current page is not
equal last page
//Enabled false Link button Last when current page is last
page
lnkbtnPgLast.Enabled = !pgsource.IsLastPage;
dlDetails.DataSource = pgsource;
dlDetails.DataBind();
DataListPagingMethod();
}
catch (Exception
ex)
{
throw ex;
}
}
protected void
lnkbtnPgFirst_Click(object sender, EventArgs e)
{
pagingCurrentPage = 0;
getGridDetails();
}
protected void
lnkbtnPgPrevious_Click(object sender, EventArgs e)
{
pagingCurrentPage -= 1;
getGridDetails();
}
protected void
lnkbtnPgNext_Click(object sender, EventArgs e)
{
pagingCurrentPage += 1;
getGridDetails();
}
protected void
lnkbtnPgLast_Click(object sender, EventArgs e)
{
pagingCurrentPage = (Convert.ToInt32(ViewState["totpage"]) - 1);
getGridDetails();
}
protected void
DataListPaging_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("Newpage"))
{
//Assign CurrentPage number when user click on the page
number in the Paging DataList
pagingCurrentPage = Convert.ToInt32(e.CommandArgument.ToString());
//Refresh DataList "DlistDetails" Data once user
change page
getGridDetails();
}
}
protected void
DataListPaging_ItemDataBound(object sender, DataListItemEventArgs e)
{
LinkButton lnkPage = (LinkButton)e.Item.FindControl("Pagingbtn");
if (lnkPage.CommandArgument.ToString() ==
pagingCurrentPage.ToString())
{
lnkPage.Enabled = false;
}
}
|
1 comment:
Thanks for your great and helpful presentation I like your good service.I always appreciate your post.That is very interesting I love reading and I am always searching for informative information like this.Well written article Thank You for Sharing with Us
Dot Net Training Institute in Chennai
Software Testing Course in Chennai
Core Java Training in Chennai
PHP Certification in Chennai
Post a Comment