.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

ASP.Net Paging

Posted By:sandeepms       Posted Date: January 07, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

This is a very best eg for paging in asp.net. Because This code is perfect eg for paging. using this code we can jump to any particular page. n when clicking previous next buttons. the page no in textbox will also be changed

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

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;

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

{

int page;

string conn = ConfigurationManager.AppSettings["dsn"];

int cpage;

protected void Page_Load(object sender, EventArgs e)

{

try

{

lblEmail.Text = Session["email"].ToString();

}

catch

{

Response.Redirect("index.aspx");

}

try

{

if (!IsPostBack)

{

hdnMess.Value = Request.QueryString["mess"];

if (hdnMess.Value != "")

{

if (hdnMess.Value == "1")

{

string str = "alert('Sorry this foods topic is already deleted');";

ClientScript.RegisterStartupScript(this.GetType(), "load", str, true);

}

else if (hdnMess.Value == "2")

{

string str = "alert('Sorry you didn't write this topic so you can't delete it');";

ClientScript.RegisterStartupScript(this.GetType(), "load", str, true);

}

else

{

lblError.Text = "";

}

}

SqlConnection cnn = new SqlConnection(conn);

SqlDataAdapter da = new SqlDataAdapter("select * from foods", cnn);

DataSet ds = new DataSet();

da.Fill(ds);

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

{

PagedDataSource pds = new PagedDataSource();

pds.DataSource = ds.Tables[0].DefaultView;

pds.AllowPaging = true;

pds.PageSize = 15;

pds.CurrentPageIndex = cpage;

cpage += 1;

txtMessage.Text = cpage.ToString();

DataList1.DataSource = pds;

DataList1.DataBind();

}

}

}

catch

{

Response.Redirect("welcome.aspx");

}

}

protected void txtFood_TextChanged(object sender, EventArgs e)

{

try

{

lblError.Text = "";

SqlConnection cnn = new SqlConnection(conn);

SqlCommand cmm = new SqlCommand("select * from foods where name like @1", cnn);

cmm.Parameters.AddWithValue("@1", txtFood.Text + "%");

SqlDataAdapter da = new SqlDataAdapter(cmm);

DataSet ds = new DataSet();

da.Fill(ds);

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

{

PagedDataSource pds = new PagedDataSource();

pds.DataSource = ds.Tables[0].DefaultView;

pds.AllowPaging = true;

pds.PageSize = 15;

if (txtMessage.Text != "")

{

page = Convert.ToInt16(txtMessage.Text);

cpage = page - 1;

int pageno = pds.PageCount + 1;

if (page > 0 && page < pageno)

{

pds.CurrentPageIndex = cpage;

DataList1.DataSource = pds;

DataList1.DataBind();

}

else

{

if (pageno - 1 != 1)

{

lblError.Text = "please enter a number between 1 and " + (pageno - 1).ToString();

}

else

{

lblError.Text = "Please change the page No text box to 0 because there is only one page in search results";

}

}

}

else

{

lblError.Text = "Please select a category";

}

}

else

{

DataList1.DataSource = "";

DataList1.DataBind();

lblError.Text = "";

}

}

catch

{

Response.Redirect("welcome.aspx");

}

}

protected void lnkLogout_Click(object sender, EventArgs e)

{

Session.Abandon();

Session.Remove("email");

Response.Redirect("index.aspx");

}

protected void txtMessage_TextChanged(object sender, EventArgs e)

{

try

{

SqlConnection cnn = new SqlConnection(conn);

SqlCommand cmm = new SqlCommand("select * from foods where name like @1", cnn);

cmm.Parameters.AddWithValue("@1", txtFood.Text + "%");

SqlDataAdapter da = new SqlDataAdapter(cmm);

DataSet ds = new DataSet();

da.Fill(ds);

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

{

PagedDataSource pds = new PagedDataSource();

pds.DataSource = ds.Tables[0].DefaultView;

pds.AllowPaging = true;

pds.PageSize = 15;

if (txtMessage.Text != "")

{

page = Convert.ToInt16(txtMessage.Text);

cpage = page - 1;

int pageno = pds.PageCount + 1;

if (page > 0 && page < pageno)

{

pds.CurrentPageIndex = cpage;

DataList1.DataSource = pds;

DataList1.DataBind();

}

else

{

if (pageno - 1 != 1)

{

txtMessage.Text = (pageno - 1).ToString();

lblError.Text = "please enter a number between 1 and " + (pageno - 1).ToString();

}

else

{

txtMessage.Text = "1";

lblError.Text = "Please change the page No text box to 1 because there is only one page in search results";

}

}

}

else

{

lblError.Text = "Please select a category";

}

}

else

{

DataList1.DataSource = "";

DataList1.DataBind();

lblError.Text = "";

}

}

catch

{

Response.Redirect("welcome.aspx");

}

}

protected void lnkFirst_Click(object sender, EventArgs e)

{

try

{

lblError.Text = "";

SqlConnection cnn = new SqlConnection(conn);

SqlCommand cmm = new SqlCommand("select * from foods where name like @1", cnn);

cmm.Parameters.AddWithValue("@1", txtFood.Text + "%");

SqlDataAdapter da = new SqlDataAdapter(cmm);

DataSet ds = new DataSet();

da.Fill(ds);

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

{

PagedDataSource pds = new PagedDataSource();

pds.DataSource = ds.Tables[0].DefaultView;

pds.AllowPaging = true;

pds.PageSize = 15;

if (txtMessage.Text != "")

{

cpage = 0;

pds.CurrentPageIndex = cpage;

DataList1.DataSource = pds;

DataList1.DataBind();

cpage += 1;

txtMessage.Text = cpage.ToString();

}

else

{

lblError.Text = "Please select a category";

}

}

else

{

DataList1.DataSource = "";

DataList1.DataBind();

lblError.Text = "";

}

}

catch

{

Response.Redirect("welcome.aspx");

}

 

}

protected void lnkPrevious_Click(object sender, EventArgs e)

{

try

{

lblError.Text = "";

SqlConnection cnn = new SqlConnection(conn);

SqlCommand cmm = new SqlCommand("select * from foods where name like @1", cnn);

cmm.Parameters.AddWithValue("@1", txtFood.Text + "%");

SqlDataAdapter da = new SqlDataAdapter(cmm);

DataSet ds = new DataSet();

da.Fill(ds);

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

{

PagedDataSource pds = new PagedDataSource();

pds.DataSource = ds.Tables[0].DefaultView;

pds.AllowPaging = true;

pds.PageSize = 15;

if (txtMessage.Text != "")

{

cpage = Convert.ToInt32(txtMessage.Text);

int lpage = pds.PageCount + 1;

if (cpage > lpage)

{

cpage = lpage;

}

else if (cpage < 1)

{

cpage = 2;

}

cpage -= 1;

if (cpage > 0)

{

pds.CurrentPageIndex = cpage - 1;

DataList1.DataSource = pds;

DataList1.DataBind();

txtMessage.Text = cpage.ToString();

}

}

else

{

lblError.Text = "Please select a category";

}

}

else

{

DataList1.DataSource = "";

DataList1.DataBind();

lblError.Text = "";

}

}

catch (Exception exc)

{

lblError.Text = exc.Message;

}

}

protected void lnkNext_Click(object sender, EventArgs e)

{

try

{

lblError.Text = "";

SqlConnection cnn = new SqlConnection(conn);

SqlCommand cmm = new SqlCommand("select * from foods where name like @1", cnn);

cmm.Parameters.AddWithValue("@1", txtFood.Text + "%");

SqlDataAdapter da = new SqlDataAdapter(cmm);

DataSet ds = new DataSet();

da.Fill(ds);

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

{

PagedDataSource pds = new PagedDataSource();

pds.DataSource = ds.Tables[0].DefaultView;

pds.AllowPaging = true;

pds.PageSize = 15;

if (txtMessage.Text != "")

{

cpage = Convert.ToInt32(txtMessage.Text);

int lpage = pds.PageCount + 1;

if (cpage > lpage)

{

cpage = 0;

}

else if (cpage < 1)

{

cpage = lpage - 2;

}

cpage += 1;

if (cpage < lpage)

{

pds.CurrentPageIndex = cpage - 1;

DataList1.DataSource = pds;

DataList1.DataBind();

txtMessage.Text = cpage.ToString();

}

else

{

string str = "alert('You are in the last page itself');";

ClientScript.RegisterStartupScript(this.GetType(), "load", str, true);

}

}

else

{

lblError.Text = "Please select a category";

}

}

else

{

DataList1.DataSource = "";

DataList1.DataBind();

lblError.Text = "";

}

}

catch (Exception exc)

{

lblError.Text = exc.Message;

}

}

protected void lnkLast_Click(object sender, EventArgs e)

{

try

{

lblError.Text = "";

SqlConnection cnn = new SqlConnection(conn);

SqlCommand cmm = new SqlCommand("select * from foods where name like @1", cnn);

cmm.Parameters.AddWithValue("@1", txtFood.Text + "%");

SqlDataAdapter da = new SqlDataAdapter(cmm);

DataSet ds = new DataSet();

da.Fill(ds);

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

{

PagedDataSource pds = new PagedDataSource();

pds.DataSource = ds.Tables[0].DefaultView;

pds.AllowPaging = true;

pds.PageSize = 15;

if (txtMessage.Text != "")

{

int pageno = pds.PageCount;

pds.CurrentPageIndex = pageno - 1;

DataList1.DataSource = pds;

DataList1.DataBind();

txtMessage.Text = pageno.ToString();

}

else

{

lblError.Text = "Please select a category";

}

}

else

{

DataList1.DataSource = "";

DataList1.DataBind();

lblError.Text = "";

}

}

catch (Exception exc)

{

lblError.Text = exc.Message;

}

}

}



     

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