I've been trying to figure out how to delete a radgrid, but it seems the best solution is to set its visibility to false and work off of that.
So i tried that, and now lets say i have 4 grids:
Grid1
Grid2
Grid3
Grid4
And i delete Grid3, and now i am left with:
Grid1
Grid2
Grid4
And i have a button that you can click on, that shows all the grids and the data in the grid, if i don't delete any grids it works fine, but if i delete one of the grids, i get the following error when i click to PreviewAll grids.
Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
Below is the code i am using to "Delete the grid" which is just setting the visibility to false:
PreviewAll grids method, which runs and shows all the grids
Any help is very much appreciated.
So i tried that, and now lets say i have 4 grids:
Grid1
Grid2
Grid3
Grid4
And i delete Grid3, and now i am left with:
Grid1
Grid2
Grid4
And i have a button that you can click on, that shows all the grids and the data in the grid, if i don't delete any grids it works fine, but if i delete one of the grids, i get the following error when i click to PreviewAll grids.
Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
Below is the code i am using to "Delete the grid" which is just setting the visibility to false:
public void DeleteGrid(object sender, EventArgs e, PlaceHolder ph)
{
LinkButton gridLink = (LinkButton)sender;
String gridNum = gridLink.ID.ToString().Split('-').Last();
System.Web.UI.Page currentPage;
currentPage = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
RadGrid grid = (RadGrid)currentPage.FindControl("RadGrid" + gridNum);
Label lbl = (Label)currentPage.FindControl("Break-" + gridNum);
LinkButton lbd = (LinkButton)currentPage.FindControl("Delete-Grid-" + gridNum);
LinkButton lb = (LinkButton)currentPage.FindControl("Show-Grid-" + gridNum);
grid.Visible = false;
lbl.Visible = false;
lbd.Visible = false;
lb.Visible = false;
HttpContext.Current.Session.Add(gridNum, null);
}
PreviewAll grids method, which runs and shows all the grids
public void PreviewAll(PlaceHolder ph)
{
ph.Controls.Clear();
for (int i = 1; i <= Convert.ToInt32(GetSession()); i++)
{
if (HttpContext.Current.Session[i.ToString()] == null)
{
HttpContext.Current.Response.Write("Session " + i.ToString() + " is null");
}
else
{
LinkButton btn = new LinkButton();
btn.ID = "Modal-" + i.ToString();
btn.Text = "Show Room " + i.ToString();
btn.Click += (sender, e) => ShowPopUp(sender, e, i, ph);
DataTable table = (DataTable)HttpContext.Current.Session[i]; // Get the data table.
Label lbl = new Label();
lbl.ID = "Room-" + i.ToString();
lbl.Text = "Room " + i.ToString();
Label lblBreak = new Label();
lblBreak.ID = "Label-Break" + i.ToString();
lblBreak.Text = "<
br
/>";
ph.Controls.Add(lbl);
ph.Controls.Add(lblBreak);
ph.Controls.Add(btn);
ph.Controls.Add(lblBreak);
foreach (DataRow row in table.Rows) // Loop over the rows.
{
foreach (var item in row.ItemArray) // Loop over the items.
{
Label lblValue = new Label();
lblValue.ID = "Label-Value-" + item.ToString();
lblValue.Text = item.ToString();
Label lblBreak2 = new Label();
lblBreak2.ID = "Label-Break" + i.ToString();
lblBreak2.Text = "<
br
/>";
ph.Controls.Add(lblValue);
ph.Controls.Add(lblBreak2);
}
}
}
}
}
Any help is very much appreciated.