How to dynamically create Link Buttons for Custom Paging in DataGrid Control
In .Net when it comes to Gridview control we want to achieve a task like Custom paging with Linkbuttons . How we achieve it ? This Article will demonstrate to handle a custom paging in Gridview control using asp.net . In this article we can know how to dynamically create Link Buttons for Paging in Data List and how to write a delegate for the Linkbutton using asp.net.
When you are working with Gridview/DataGrid Control
We want to work with custom paging (page numbers , Previous ,next buttons and current page will shows with page number surrounded by this [] symbol for ex : if you click page no 1 than it will shows you [ 1 ] )
This Article will show you to
1)create dynamically Link buttons for your Datalist/Datagrid/Gridview(for your custom Paging) and
2) creating event delegate for the Link Control.
Our first and foremost step is to create Link Buttons dynamically to the form that is done with below code snippet.
so how many LinkButtons we need to create in the form. I just need to query from the database. If you have rowids column you can simply use RowId Column to create Linkbuttons dynamically if you don't have rowids for your records in the table use rowNumber function to create rowids for your records. The below code snippet will
illustrates that.
SELECT ROW_NUMBER() OVER(ORDER BY imgpath) AS RowID,
imgpath from tblimages IS NOT NULL;
So i fetch all resultset in Dataset
Create a Link buttons in your form page
private void CreateLinkButtons()
{
dt =(DataTable)ViewState["dtsql"];
if(dt.Rows.Count >0)
{
Count = dt.Rows.Count;
totalpageval = Count%pagesize;
if(totalpageval ==0)
{
totalpages = Count/pagesize;
}
else
{
totalpages = Count/pagesize;
totalpages = totalpages + 1 ;
}
TableRow tr =new TableRow();
tr.ID ="ctrl" ;
Table1.Rows.Add(tr);
for(int x=1;x<=totalpages;x++)
{
if(x==1)
{
lnk=new LinkButton();
TableCell tc =new TableCell();
tr.Cells.Add(tc);
btnprev =new Button();
btnprev.ID ="btnPrev";
btnprev.Text ="Previous";
tc.Controls.Add(btnprev);
btnprev.Enabled =false;
TableCell tc1 =new TableCell();
tr.Cells.Add(tc1);
lnk.Text= "[" + x.ToString() + "]";
lnk.ID = x.ToString();
tc1.Controls.Add(lnk);
lnk.Enabled =false;
lnk.Click += new System.EventHandler(this.lnk_Click);
btnprev.Click += new System.EventHandler(this.btnprev_Click);
}
if(x!=1 && x<=7)
{
lnk=new LinkButton();
TableCell tc =new TableCell();
tr.Cells.Add(tc);
lnk.Text= x.ToString();
lnk.ID = x.ToString();
tc.Controls.Add(lnk);
Table3.Rows.Add(trnew);
TableCell tcnews =new TableCell();
trnew.Cells.Add(tcnews);
btnnext.Text="Next";
tcnews.Controls.Add(btnnext);
btnnext.Enabled =true;
Table3.Visible =true;
lnk.Click += new System.EventHandler(this.lnk_Click);
btnnext.Click += new System.EventHandler(this.btnnext_Click);
}
if(x>7)
{
Table3.Visible =true;
lnk=new LinkButton();
TableCell tc =new TableCell();
tr.Cells.Add(tc);
lnk.Text= x.ToString();
lnk.ID = x.ToString();
lnk.Visible =false;
tc.Controls.Add(lnk);
lnk.Click += new System.EventHandler(this.lnk_Click);
}
}
}
}
To create Dynamical Link buttons In .Net we have Linkbutton class using this we can add Linkbuttons we can add those linkbuttons and with ID's and text as attributes In Id's and text i set the attributes of the Page Numbers which are my rowid' s in dataset and finally we can add those linkbuttons to the asp.net table with row and column.
After adding the Link buttons Dynamically we need to write the Delegate of the Link button like this . So what it will identify which page number we have clicked or in other words the clicked Linkbutton (page number) to the code that we already premeditated by setting text and id properties as page numbers.
lnk.Click += new System.EventHandler(this.lnk_Click);
private void lnk_Click(object sender,System.EventArgs e)
{
}
Here in this Program not only i used paging with numbers and use previous and next Link buttons to navigate the DataList. Suppose if you are in Current page is 5 and while clicking the Next Linkbutton it will move to page 6 and while clicking Previous it moves to 4 (if the current page is 5). with [ ] indicates the Current page where you are.
The Entire Code is given below
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace WebApplication5
{
///
/// Summary description for WebForm1.
///
public class WebForm1 : System.Web.UI.Page
{
DataTable dt =new DataTable();
DataTable dtBind =new DataTable();
LinkButton lnk ;
int pagesize = 4;
int totalpages =0;
int totalpageval=0;
bool flag =false;
TableRow trnew =new TableRow();
Button btnnext =new Button();
int Count=0;
bool flagnext =false;
Button btnprev;
bool btnprevs=true;
static ArrayList arrremve =new ArrayList();
protected System.Web.UI.WebControls.Table Table1;
protected System.Web.UI.WebControls.Table Table3;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
else
{
CreateLinkButtons();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
private void BindGrid()
{
SqlConnection con =new SqlConnection("Server=HOME-28C27D1FD2;uid=sa;pwd=sa;DataBase=Master");
con.Open();
SqlDataAdapter adpt =new SqlDataAdapter("select rowid =count(i.imgpath),i.imgpath from tblimgs i join tblimgs k on i.imgpath > =k.imgpath group by(i.imgpath) ",con);
adpt.Fill(dt);
ViewState["dtsql"] = dt;
DataGrid1.DataSource =dt;
DataGrid1.DataBind();
CreateLinkButtons();
}
private void CreateLinkButtons()
{
dt =(DataTable)ViewState["dtsql"];
if(dt.Rows.Count >0)
{
Count = dt.Rows.Count;
totalpageval = Count%pagesize;
if(totalpageval ==0)
{
totalpages = Count/pagesize;
}
else
{
totalpages = Count/pagesize;
totalpages = totalpages + 1 ;
}
TableRow tr =new TableRow();
tr.ID ="ctrl" ;
Table1.Rows.Add(tr);
for(int x=1;x<=totalpages;x++)
{
if(x==1)
{
lnk=new LinkButton();
TableCell tc =new TableCell();
tr.Cells.Add(tc);
btnprev =new Button();
btnprev.ID ="btnPrev";
btnprev.Text ="Previous";
tc.Controls.Add(btnprev);
btnprev.Enabled =false;
TableCell tc1 =new TableCell();
tr.Cells.Add(tc1);
lnk.Text= "[" + x.ToString() + "]";
lnk.ID = x.ToString();
tc1.Controls.Add(lnk);
lnk.Enabled =false;
lnk.Click += new System.EventHandler(this.lnk_Click);
btnprev.Click += new System.EventHandler(this.btnprev_Click);
}
if(x!=1 && x<=7)
{
lnk=new LinkButton();
TableCell tc =new TableCell();
tr.Cells.Add(tc);
lnk.Text= x.ToString();
lnk.ID = x.ToString();
tc.Controls.Add(lnk);
Table3.Rows.Add(trnew);
TableCell tcnews =new TableCell();
trnew.Cells.Add(tcnews);
btnnext.Text="Next";
tcnews.Controls.Add(btnnext);
btnnext.Enabled =true;
Table3.Visible =true;
lnk.Click += new System.EventHandler(this.lnk_Click);
btnnext.Click += new System.EventHandler(this.btnnext_Click);
}
if(x>7)
{
Table3.Visible =true;
lnk=new LinkButton();
TableCell tc =new TableCell();
tr.Cells.Add(tc);
lnk.Text= x.ToString();
lnk.ID = x.ToString();
lnk.Visible =false;
tc.Controls.Add(lnk);
lnk.Click += new System.EventHandler(this.lnk_Click);
}
}
}
}
private void btnnext_Click(object sender,System.EventArgs e)
{
ArrayList arrcheck =new ArrayList();
arrcheck.Clear() ;
if(flagnext !=true)
{
for(int x =1;x<=totalpages;x++)
{
System.Web.UI.Control ctrl =Table1.FindControl(x.ToString());
lnk = (LinkButton)ctrl;
if(lnk.Visible ==true)
{
if(arrcheck.Count==0)
{
arrcheck.Add(lnk.Text);
}
else
{
arrcheck.Insert(arrcheck.Count,lnk.Text);
}
}
}
}
int arrcheckcount = arrcheck.Count + 1 ;
if(arrcheckcount<=totalpages && flagnext !=true)
{
System.Web.UI.Control ctrl =Table1.FindControl(arrcheckcount.ToString());
if(ctrl.ID!="")
{
lnk = (LinkButton)ctrl;
lnk.Visible =true;
arrcheck.Clear();
}
}
if(arrremve!=null&& btnprevs ==true)
{
if(arrremve.Count > 0)
{
int aacount = arrremve.Count;
System.Web.UI.Control ctrl =Table1.FindControl(arrremve[aacount-1].ToString());
lnk = (LinkButton)ctrl;
if(lnk.Visible ==false)
{
lnk.Visible =true;
arrremve.Remove(arrremve[aacount-1]);
btnprevs =false;
if(arrremve.Count==0)
{
arrremve.Clear();
}
return;
}
}
}
for(int x=1;x<=totalpages;x++)
{
if(flag ==false)
{
System.Web.UI.Control ctrl =Table1.FindControl(x.ToString());
lnk = (LinkButton)ctrl;
lnk.Visible =true;
if(lnk.Text.StartsWith("["))
{
lnk.Text =lnk.Text.Replace("["," ");
lnk.Text =lnk.Text.Replace("]"," ");
int incre = int.Parse(lnk.Text) + 1;
BindGrids(incre.ToString());
if(totalpages == incre)
{
flagnext =true;
btnnext.Enabled =false;
}
else
{
flagnext =true;
btnnext.Enabled =true;
}
}
}
else
{
break;
}
}
}
private void btnprev_Click(object sender,System.EventArgs e)
{
string ctrltext=string.Empty;
for(int x=8;x<=totalpages;x++)
{
System.Web.UI.Control ctrl =Table1.FindControl(x.ToString());
lnk = (LinkButton)ctrl;
if(lnk.Visible ==true)
{
ctrltext = x.ToString();
}
}
if(ctrltext!="")
{
System.Web.UI.Control ctrls =Table1.FindControl(ctrltext.ToString());
lnk = (LinkButton)ctrls;
if(lnk.Visible ==true)
{
lnk.Visible =false;
if(arrremve.Count == 0)
{
arrremve.Add(ctrltext);
}
else
{
arrremve.Insert(arrremve.Count,ctrltext);
}
}
btnprevs =true;
}
for(int x=1;x<=totalpages;x++)
{
if(flag ==false)
{
System.Web.UI.Control ctrl =Table1.FindControl(x.ToString());
lnk = (LinkButton)ctrl;
lnk.Visible =true;
if(lnk.Text.StartsWith("["))
{
lnk.Text =lnk.Text.Replace("["," ");
lnk.Text =lnk.Text.Replace("]"," ");
int incre = int.Parse(lnk.Text) - 1 ;
BindGrids(incre.ToString());
}
}
else
{
return;
}
}
}
private void lnk_Click(object sender,System.EventArgs e)
{
for(int x=1;x<=totalpages;x++)
{
System.Web.UI.Control ctrl =Table1.FindControl(x.ToString());
if(ctrl !=null)
{
lnk = (LinkButton)ctrl;
lnk.Enabled =true;
lnk.Text = lnk.Text.Replace("["," ");
lnk.Text = lnk.Text.Replace("]"," ");
}
}
lnk = (LinkButton)sender;
BindGrids(lnk.Text);
}
private void BindGrids(string currentpage)
{
int startpage=0;
int endpage =0;
int currpage =0;
DataRow [] drow =null;
dt = (DataTable)ViewState["dtsql"];
currpage = int.Parse(currentpage);
if(currpage ==1)
{
startpage = 1;
endpage =currpage + 4 -1 ;
}
else
{
startpage = currpage *4 -4 + 1;
endpage = startpage + 4 - 1;
}
string filt = "rowid > = '"+ startpage +"' and rowid <= '"+ endpage + "'";
drow =dt.Select(filt);
dtBind = BindDatatable(drow);
DataGrid1.DataSource =dtBind;
DataGrid1.DataBind();
for(int x=1;x<=totalpages;x++)
{
System.Web.UI.Control ctrl =Table1.FindControl(x.ToString());
if(ctrl !=null)
{
lnk = (LinkButton)ctrl;
lnk.Enabled =true;
lnk.Text = lnk.Text.Replace("["," ");
lnk.Text = lnk.Text.Replace("]"," ");
lnk.Text =lnk.Text.Trim();
}
}
System.Web.UI.Control ctrls =Table1.FindControl(currentpage);
if(ctrls.ID!=null)
{
lnk= (LinkButton)ctrls;
lnk.Enabled =false;
lnk.Text ="[" + lnk.Text + "]";
lnk.Click += new System.EventHandler(this.lnk_Click);
flag =true;
}
string originallink = lnk.Text.Remove(0,1);
originallink =originallink.Remove(originallink.Length-1,1);
if(originallink.Trim()!="1")
{
btnprev.Enabled =true;
}
if(originallink.Trim() !=totalpages.ToString())
{
btnnext.Enabled =true;
return;
}
else
{
btnnext.Enabled=false;
return;
}
}
private DataTable BindDatatable(DataRow []dr)
{
DataTable dtnew =new DataTable();
DataRow drnew =null;
DataColumn dc =new DataColumn("imgpath");
dtnew.Columns.Add(dc);
for(int i=0; i< dr.Length;i++)
{
drnew = dtnew.NewRow();
drnew["imgpath"]= dr[i].ItemArray[1].ToString();
dtnew.Rows.Add(drnew) ;
}
return dtnew;
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound_1);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_ItemDataBound_1(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType ==ListItemType.Item)
{
if(dtBind.Rows.Count>0)
{
dt =null;
dt =dtBind;
}
for(int i=0;i
ImageButton img =new ImageButton();
img = (ImageButton)e.Item.FindControl("imgBhushan");
if(img.ImageUrl=="")
{
img.ImageUrl ="~/image2/" + dt.Rows[i]["imgpath"].ToString();
}
else
{
ImageButton img1 =new ImageButton();
img1 = (ImageButton)e.Item.FindControl("Image1");
if(img1.ImageUrl=="")
{
img1.ImageUrl ="~/image2/" + dt.Rows[i]["imgpath"].ToString();
}
else
{
ImageButton img2 =new ImageButton();
img2 = (ImageButton)e.Item.FindControl("Image2");
if(img2.ImageUrl=="")
{
img2.ImageUrl = "~/image2/" + dt.Rows[i]["imgpath"].ToString();
}
else
{
ImageButton img3 =new ImageButton();
img3 = (ImageButton)e.Item.FindControl("Image3");
if(img3.ImageUrl=="")
{
img3.ImageUrl ="~/image2/" + dt.Rows[i]["imgpath"].ToString();
}
}
}
}
}
}
}
}
}
Final Note :
The Code may not get the exact result but it will definitely give a broad picture how to achieve this type of tasks.
Nice code and description