In this article i am explaining how to do paging in datalist
as we know datalist control does not support paging properties as Gridview
control support. In this article i m displaying two images for paging and
according to your requirement you can change it. First of all create a database following the
script which you can copy and paste it in your sql server 2005.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tbStudent]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tbStudent](
[id] [int] IDENTITY(10,1) NOT NULL,
[name] [varchar](50) NULL,
[age] [int] NULL,
[rollno] [int] NULL,
[address] [varchar](50) NULL,
[image] [varchar](50) NULL
) ON [PRIMARY]
END
Now create a folder name pics in your website.
Few property must be set like
Repeatcolumns =2 or you can change it
Now add a page name it dtlist.aspx
and paste the souce code in it.
<%@ Page Language="C#"
AutoEventWireup="true" CodeFile="dtlist.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>
<style type="text/css">
.style1
{
width:
85%;
height:
67px;
}
.style2
{
width:
770px;
}
.style3
{
width:
76px;
}
.style4
{
width:
67px;
}
.style5
{
width:
67px;
height:
21px;
}
.style6
{
height:
21px;
}
.style7
{
width:
62px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td class="style2">
<table class="style1">
<tr>
<td class="style7">
Name</td>
<td>
<asp:TextBox ID="txt_name"
runat="server"
style="margin-bottom: 0px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style7">
age</td>
<td>
<asp:TextBox ID="txt_age"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style7">
rollno</td>
<td>
<asp:TextBox ID="txt_rollno"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style7">
address</td>
<td>
<asp:TextBox ID="txt_address"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style7">
image</td>
<td>
<asp:FileUpload ID="fileupload"
runat="server"
/>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ErrorMessage="Invalid File!(only
.gif, .jpg, .jpeg, .wav Files are supported)"
ValidationExpression="^.+(.jpg|.JPG|.gif|.GIF|.jpeg|JPEG)$"
ControlToValidate="fileupload"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style7">
</td>
<td>
<asp:Button ID="Button1"
runat="server"
onclick="Button1_Click"
Text="Insert"
/>
</td>
</tr>
</table>
</td>
<td class="style2">
</td>
<td>
</td>
</tr>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="2" Width="500px"
CellPadding="4" ForeColor="#333333"
Height="204px"
ShowFooter="False" ShowHeader="False">
<FooterStyle BackColor="#990000"
Font-Bold="True"
ForeColor="White"
/>
<ItemTemplate>
<table class="style1">
<tr>
<td rowspan="4" class="style3">
<img src='pics/<%#Eval("image")
%>' style="height: 95px; width: 93px" />
</td>
<td class="style4">
name</td>
<td>
<asp:Label ID="Label1" runat="server"
Text='<%#
Eval("name") %>'></asp:Label>
</td>
</tr>
<tr>
<td class="style4">
age</td>
<td>
<asp:Label ID="Label2" runat="server" Text='<%#
Eval("age") %>' Width="200px"></asp:Label>
</td>
</tr>
<tr>
<td class="style5">
rollno</td>
<td class="style6">
<asp:Label ID="Label3" runat="server"
Text='<%#
Eval("rollno") %>'></asp:Label>
</td>
</tr>
<tr>
<td class="style4">
address</td>
<td>
<asp:Label ID="Label4" runat="server"
Text='<%#
Eval("address") %>'
Width="250px"></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
<AlternatingItemStyle BackColor="White"
/>
<ItemStyle BackColor="#FFFBD6"
ForeColor="#333333"
/>
<SelectedItemStyle BackColor="#FFCC66"
Font-Bold="True"
ForeColor="Navy"
/>
<HeaderStyle BackColor="#990000"
Font-Bold="True"
ForeColor="White"
/>
</asp:DataList>
</table>
</div>
<p>
<asp:LinkButton ID="btn_prev" runat="server" onclick="btn_prev_Click">Prev</asp:LinkButton>
<asp:LinkButton ID="btn_first" runat="server" onclick="btn_first_Click">First</asp:LinkButton>
<asp:LinkButton ID="btn_nxt" runat="server" onclick="btn_nxt_Click">Next</asp:LinkButton>
<asp:LinkButton ID="btn_last" runat="server" onclick="btn_last_Click">Last</asp:LinkButton>
</p>
<p>
<asp:Label ID="lbl_msg" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
In Coding
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;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
decimal
count; //for count the total records.
Decimal
last1; // to access the last record
String
fn; // for image name
String
pth; // for storing the path
SqlConnection
con = new SqlConnection();
SqlCommand
cmd;
PagedDataSource
pageData = new PagedDataSource();
protected void Page_Load(object
sender, EventArgs e)
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
con.Open();
doPaging(); //will
call this message and fetch the data from database
}
public DataTable getTheData()
{
SqlDataAdapter
objSQLAdapter = new SqlDataAdapter("select *from [tbStudent]", con);
DataSet
DS = new DataSet();
objSQLAdapter.Fill(DS);
if
(DS.Tables[0].Rows.Count == 0)
{
lbl_msg.Text = "";
DataList1.Visible = false;
}
else
{
Session["cnt"]
= DS.Tables[0].Rows.Count;
}
return DS.Tables[0];
}
private void doPaging()
{
//paging
function
try
{
pageData.DataSource =
getTheData().DefaultView; //will call the
getTheData() method
pageData.AllowPaging = true;
pageData.PageSize = 2;
count = Convert.ToInt32(Session["cnt"]);
last1 = count / pageData.PageSize;
last1 = Convert.ToDecimal(Math.Ceiling(last1)); //
will convert the 17/4 = 4.5 --> 5 pages
try
{
pageData.CurrentPageIndex = Int32.Parse(Request["page"].ToString());
}
catch
{
pageData.CurrentPageIndex = 0; // default first 4 images
}
btn_prev.Visible =
(!pageData.IsFirstPage);
btn_first.Visible =
(!pageData.IsFirstPage);
btn_nxt.Visible =
(!pageData.IsLastPage);
btn_last.Visible =
(!pageData.IsLastPage);
DataList1.DataSource = pageData;
DataList1.DataBind();
}
catch
{
}
}
protected void Button1_Click(object
sender, EventArgs e)
{
if
(fileupload.PostedFile.ContentLength > 0)
{
fn = Path.GetFileName(fileupload.FileName);
pth = Server.MapPath("pics") + "/"
+ fn; // fn for file image name
fileupload.SaveAs(pth); // storing the image in pics folder *Be sure pics folder
must exist there..
}
if
(con.State == ConnectionState.Closed)
{
con.Open();
}
cmd = new
SqlCommand("insert
into [tbStudent] values(@name,@age,@rollno,@address,@image)", con);
cmd.Parameters.AddWithValue("@name", txt_name.Text);
cmd.Parameters.AddWithValue("@age", txt_age.Text);
cmd.Parameters.AddWithValue("@rollno", txt_rollno.Text);
cmd.Parameters.AddWithValue("@address", txt_address.Text);
cmd.Parameters.AddWithValue("@image", fn);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
doPaging();
}
protected void btn_prev_Click(object
sender, EventArgs e)
{
Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + (pageData.CurrentPageIndex -
1).ToString());
}
protected void btn_first_Click(object
sender, EventArgs e)
{
Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + (pageData.CurrentPageIndex ==
1).ToString());
}
protected void btn_nxt_Click(object
sender, EventArgs e)
{
Response.Redirect("dtlist.aspx?page=" + Convert.ToInt32(last1 - 1)); // dtlist.aspx to access the last record.
}
protected void btn_last_Click(object
sender, EventArgs e)
{
Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + (pageData.CurrentPageIndex +
1).ToString());
}
}
No comments:
Post a Comment