.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 >> Javascript >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

DataListPaging in ASP.NET

Posted By:Athira Sreejith       Posted Date: February 22, 2009    Points: 5    Category: Javascript    URL: http://www.dotnetspark.com  
 

This code is explaning about DataListPaging in ASP.NET .aspx: Customers First Previous Next Last Company: Contact: Company: Contact: .cs Page: using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class datalistpaging : System.Web.UI.Page { int curpage, pagesize = 10; DataTable dt1 = new DataTable(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { showfirst(this, System.EventArgs.Empty); } } public void loaddata() { if (ViewState["Data"] == null) { SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]); con.Open(); string qry = "select * from Customers order by CompanyName"; SqlDataAdapter ad = new SqlDataAdapter(qry, con); ad.Fill(dt1); ViewState["Data"] = dt1; } else { dt1 = (DataTable)ViewState["Data"]; } } private void showpage() { int i; loaddata(); DataTable dt2 = dt1.Clone(); for (i = ((curpage - 1) * pagesize); (i < curpage * pagesize && i <= dt1.Rows.Count - 1); i++) { dt2.ImportRow(dt1.Rows[i]); } DataList1.DataSource = dt2; DataList1.DataBind(); ((Label)DataList1.Controls[DataList1.Controls.Count - 1].FindControl("StatusLabel")).Text = "Total Records: " + dt1.Rows.Count + " . Page " + curpage + "of" + System.Math.Ceiling((double)dt1.Rows.Count / pagesize) + "."; ViewState["CurrentPage"] = curpage; } protected void showfirst(object sender, System.EventArgs e) { curpage = 1; showpage(); } protected void shownext(object sender, System.EventArgs e) { loaddata(); curpage = Int32.Parse(ViewState["CurrentPage"].ToString()); if (curpage <= System.Math.Ceiling((double)dt1.Rows.Count / pagesize)) curpage += 1; showpage(); } protected void showlast(object sender, System.EventArgs e) { loaddata(); curpage = (int)System.Math.Ceiling((double)dt1.Rows.Count / pagesize); showpage(); } protected void showprevious(object sender, System.EventArgs e) { curpage = Int32.Parse(ViewState["CurrentPage"].ToString()); if (curpage > 1) { curpage -= 1; } showpage(); } }

     

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