Products.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Data.SqlClient;
public partial class Products : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
SqlDataAdapter adp = new SqlDataAdapter();
SqlCommand cmd;
DataTable tb;
DataTable dt = new DataTable();
//string a, b;
//string a, save, fn, b, aguid,fn1,aguid1,save1;
private PagedDataSource pagedData = new PagedDataSource();
decimal last1;
Int32 count, no;
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
try
{
con.Open();
//TeSection_txt.Text = Request.QueryString["a"].ToString();
if (Page.IsPostBack == false)
{
try
{
doPaging();
}
catch
{
}
}
}
catch
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
}
con.Close();
}
//fetching the records
public DataTable getTheData()
{
dt = new DataTable();
adp = new SqlDataAdapter("select * from table_1", con);
adp.Fill(dt);
adp.Dispose();
DataList1.DataSource = dt;
DataList1.DataBind();
if (dt.Rows.Count == 0)
{
//lbl_msg.Text = “Gallery is empty “;
}
else
{
Session["cnt"] = Convert.ToInt32((dt.Rows.Count));
}
return dt;
}
private void doPaging()
{
//calling the getTheData() and fillin into pagedDataSource.
pagedData.DataSource = getTheData().DefaultView;
pagedData.AllowPaging = true;
pagedData.PageSize = 3;
count = Convert.ToInt32(Session["cnt"]);
last1 = count / pagedData.PageSize; //total record
if (count % pagedData.PageSize == 0)
{
last1 = -last1;
}
last1 = Math.Ceiling(last1);
// Last record this is becaz if u have 3 record and displayin 2 in one page the 3rd on will display on another page….
try
{
pagedData.CurrentPageIndex = Int32.Parse(Request["Page"].ToString());
}
catch
{
pagedData.CurrentPageIndex = 0;
}
//for paging u can use following code.
//Prv_btn.Visible = (!pagedData.IsFirstPage);
//first_btn.Visible = (!pagedData.IsFirstPage);
//Next_btn.Visible = (!pagedData.IsLastPage);
//Last_btn.Visible = (!pagedData.IsLastPage);
DataList1.DataSource = pagedData;
DataList1.DataBind();
}
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}