This is a migrated thread and some comments may be shown as answers.

Issue with setting grid to false

4 Answers 47 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brad
Top achievements
Rank 1
Brad asked on 09 Oct 2013, 08:05 PM
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:

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.

4 Answers, 1 is accepted

Sort by
0
Brad
Top achievements
Rank 1
answered on 11 Oct 2013, 01:05 PM
why does this happen?
0
Angel Petrov
Telerik team
answered on 14 Oct 2013, 11:55 AM
Hi Brad,

Generally the described problem will occur in situations where the control structure is not properly reconstructed after a postback. Could you ensure that this is not the case? Since the grid is just being hidden this should not cause an issue.

What I see as potentially problematic is if the logic in the PreviewAll method is executed after PageLoad. This may break the functionality because modifying the controls structure after PageLoad is not supported. As I see from support ticket 739755 it seems that the issue is resolved. If that is the case I would like to ask you to share your findings with the community so others can take benefit.

Regards,
Angel Petrov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Brad
Top achievements
Rank 1
answered on 14 Oct 2013, 01:59 PM
Thank you for your response but the ticket has not yet been resolved, i have ticket 746313 open on this issue.
0
Angel Petrov
Telerik team
answered on 17 Oct 2013, 11:16 AM
Hello Brad,

I see that my colleague Viktor has already addressed the issue. In order to avoid duplicate post I suggest that we continue our communication in the official support ticket. Once the problem is resolved you can share your finding with the community so others can take benefit.

Regards,
Angel Petrov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Brad
Top achievements
Rank 1
Answers by
Brad
Top achievements
Rank 1
Angel Petrov
Telerik team
Share this question
or