I have a page that dynamically creates gridviews on button click which that works fine. And along with it creating a grid, on the top of the grid there is a linkbutton to delete it. Now, what i am trying to accomplish is that when i delete one of the grids it will delete the grid from the page and resync the numbers. Meaning, lets say i have the following:
Grid1
Grid2
Grid3
Grid4
If i decide to delete Grid3, what actually happens is this:
Grid1
Grid2
Grid4
And then if i click something on the page for it to reload or something, then it will resync the numbers correctly:
Grid1
Grid2
Grid3
I have no idea why its doing that and why it doesn't do it this way in the first place. Can someone please help me with this?
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_Init(object sender, EventArgs e)
{
string ctrlname = this.Page.Request.Params.Get("__EVENTTARGET");
if (Session["Tables"] == null)
{
Session.Add("Tables", 1);
DataTable dt = GetTable();
Session.Add(Session["Tables"].ToString(), dt);
GridView gv = new GridView();
gv.ID = "Grid-" + Session["Tables"].ToString();
gv.DataSource = dt;
gv.DataBind();
LinkButton lb = new LinkButton();
lb.ID = "Delete-Grid-" + Session["Tables"].ToString();
lb.Text = "Delete Grid " + Session["Tables"].ToString();
lb.Click += new EventHandler(DeleteGrid);
PlaceHolder1.Controls.Add(lb);
PlaceHolder1.Controls.Add(gv);
}
if (IsPostBack)
{
PlaceHolder1.Controls.Clear();
int next = (int)Session["Tables"];
for (int i = 1; i <= (int)Session["Tables"]; i++)
{
GridView gv = new GridView();
gv.ID = "Grid-" + i.ToString();
gv.DataSource = (DataTable)Session[i];
gv.DataBind();
LinkButton lb = new LinkButton();
lb.ID = "Delete-Grid-" + i.ToString();
lb.Text = "Delete Grid " + i.ToString();
lb.Click += new EventHandler(DeleteGrid);
PlaceHolder1.Controls.Add(lb);
PlaceHolder1.Controls.Add(gv);
}
if (ctrlname == "Button1")
{
next = next + 1;
Session["Tables"] = next;
DataTable dt = GetTable();
Session.Add(Session["Tables"].ToString(), dt);
GridView gv = new GridView();
gv.ID = "Grid-" + next.ToString();
gv.DataSource = (DataTable)Session[next];
gv.DataBind();
LinkButton lb = new LinkButton();
lb.ID = "Delete-Grid-" + next.ToString();
lb.Text = "Delete Grid " + next.ToString();
lb.Click += new EventHandler(DeleteGrid);
PlaceHolder1.Controls.Add(lb);
PlaceHolder1.Controls.Add(gv);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void DeleteGrid(object sender, EventArgs e)
{
LinkButton gridLink = (LinkButton)sender;
String gridNum = gridLink.ID.ToString().Split('-').Last();
GridView grid = (GridView)this.Page.FindControl("Grid-" + gridNum);
LinkButton lbd = (LinkButton)this.Page.FindControl("Delete-Grid-" + gridNum);
PlaceHolder1.Controls.Remove(grid);
PlaceHolder1.Controls.Remove(lbd);
int next = (int)Session["Tables"];
next = next - 1;
Session.Add("Tables", next);
Session.Remove(gridNum);
}
public DataTable GetTable()
{
//
// Here we create a DataTable with a few columns.
//
// Create Datatable to store all colums
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Size", typeof(string)));
dt.Columns.Add(new DataColumn("Description", typeof(string)));
dt.Columns.Add(new DataColumn("Quantity", typeof(string)));
dt.Columns.Add(new DataColumn("Unit", typeof(string)));
dt.Columns.Add(new DataColumn("Duration", typeof(string)));
dt.Columns.Add(new DataColumn("DurationType", typeof(string)));
dt.Columns.Add(new DataColumn("Amount", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Size"] = string.Empty;
dr["Description"] = string.Empty;
dr["Quantity"] = string.Empty;
dr["Unit"] = string.Empty;
dr["Duration"] = string.Empty;
dr["DurationType"] = string.Empty;
dr["Amount"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["RowNumber"] = 2;
dr["Size"] = string.Empty;
dr["Description"] = string.Empty;
dr["Quantity"] = string.Empty;
dr["Unit"] = string.Empty;
dr["Duration"] = string.Empty;
dr["DurationType"] = string.Empty;
dr["Amount"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["RowNumber"] = 3;
dr["Size"] = string.Empty;
dr["Description"] = string.Empty;
dr["Quantity"] = string.Empty;
dr["Unit"] = string.Empty;
dr["Duration"] = string.Empty;
dr["DurationType"] = string.Empty;
dr["Amount"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["RowNumber"] = 4;
dr["Size"] = string.Empty;
dr["Description"] = string.Empty;
dr["Quantity"] = string.Empty;
dr["Unit"] = string.Empty;
dr["Duration"] = string.Empty;
dr["DurationType"] = string.Empty;
dr["Amount"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["RowNumber"] = 5;
dr["Size"] = string.Empty;
dr["Description"] = string.Empty;
dr["Quantity"] = string.Empty;
dr["Unit"] = string.Empty;
dr["Duration"] = string.Empty;
dr["DurationType"] = string.Empty;
dr["Amount"] = string.Empty;
dt.Rows.Add(dr);
return dt;
}
}