The code snippet is an example that uses an ASP.NET DataList Control with Next And Previous Buttons. The buttons help users to navigate records.
DBHelper is a class that connects to the Database. PagedDataSource is used to add paging for DataList.PageCount is obtained from ViewState.
DBHelper objDBHelper; PagedDataSource pgddata; public int PageCount { get { object o = this.ViewState["_PageCount"]; if (o == null) return 0; else return (int)o; } set { ViewState["_PageCount"] = value; } }
protected void Page_Load(object sender, EventArgs e) { objDBHelper = new DBHelper(); if (!IsPostBack) { FillDataList(true); } else { FillDataList(false); } }
public void FillDataList(bool a) { if (a) PageCount = 0; lbtnNext.Enabled = true; lbtnNext2.Enabled = true; lbtnPrevious.Enabled = true; lbtnPrevious2.Enabled = true;
string cmd = string.Format("SELECT a, c,b,d,e,f,g,h FROM table WHERE z >'{0}'", DateTime.Now); DataTable BTab = objDBHelper.GetReaderTable(cmd, 7); if (BTab.Rows.Count > 0) { if (a) PageCount = 0; lbtnNext.Enabled = true; lbtnNext2.Enabled = true; lbtnPrevious.Enabled = true; lbtnPrevious2.Enabled = true;
DataColumn newcolm = new DataColumn("name", typeof(string)); DataColumn newcolm1 = new DataColumn("bookme", typeof(string)); DataColumn calCol=new DataColumn ("viewcal",typeof(string));
BTab.Columns.Add(newcolm); BTab.Columns.Add(newcolm1); BTab.Columns.Add(calCol);
for (int i = 0; i < BTab.Rows.Count; i++) { BTab.Rows[i]["name"] = (object)objDBHelper.ExecuteScalar(string.Format("SELECT a FROM table WHERE d='{0}'", BTab.Rows[i][1].ToString())); BTab.Rows[i]["bookme"] = string.Format("page.aspx?xy={0}&gud={1}", BTab.Rows[i][0].ToString(), BTab.Rows[i][1].ToString()); BTab.Rows[i]["viewcal"] = string.Format("page2.aspx?pd={0}&zid={1}", BTab.Rows[i][1].ToString(), BTab.Rows[i][0].ToString()); } pgddata = new PagedDataSource(); pgddata.DataSource = BTab.DefaultView; pgddata.AllowPaging = true; pgddata.CurrentPageIndex = PageCount; pgddata.PageSize = 5; lbtnPrevious2.Visible = !pgddata.IsFirstPage; lbtnPrevious.Visible = !pgddata.IsFirstPage; lbtnNext2.Visible = !pgddata.IsLastPage; lbtnNext.Visible = !pgddata.IsLastPage; dlClubList.DataSource = pgddata; dlClubList.DataBind(); } } protected void lbtnPrevious_Click(object sender, EventArgs e) { PageCount -= 1; FillDataList(false); } protected void lbtnNext_Click(object sender, EventArgs e) { PageCount += 1; FillDataList(false); }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|