in your foreach scoope you are setting Viewstate only with last value from database
Panel1.Controls.Add(table);
ViewState["page_id"] = dr["page_id"].ToString();
ViewState["div"] = dr["div"].ToString();
my advice create new property for your linkbutton and set this prop in the for scoope.
i am giving a small example.
public class myButton : LinkButton
{
public myButton() : base() { }
private string _divId;
public string DivId
{
get { return _divId; }
set { _divId = value; }
}
private string _pageId;
public string PageId
{
get { return _pageId; }
set { _pageId = value; }
}
}
change this line in your for statement
LinkButton linkbut = new LinkButton();
linkbut.Text = "Delete";
linkbut.Click += new System.EventHandler(this.delete_record);
tc2.Controls.Add(linkbut);
like this
myButton linkbut = new myButton ();
linkbut.Text = "Delete";
linkbut.PageId = dr["page_id"].ToString();
linkbut.DivId = dr["div"].ToString();
linkbut.Click += new System.EventHandler(this.delete_record);
tc2.Controls.Add(linkbut);
and get div and page in your Click event like this
myButton btn = (myButton)sender;
cmd1.Parameters["@page_id"].Value = myButton.PageId;
cmd1.Parameters["@div_id"].Value = myButton.DivId;
i hope this will help you