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

[Solved] GridTemplateColumn dissapears after postback

3 Answers 201 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ion
Top achievements
Rank 1
Ion asked on 06 Apr 2009, 08:17 AM
I am using Telerik.Web.UI 2009.1.311.20. I have a grid in a master page and I am creating its structure and populating it from the content pages. This is the code I use in a content page:

protected new void Page_Load(object sender, EventArgs e) 
    { 
         
        ContentPlaceHolder cph = Master.Master.FindControl("PageContents"as ContentPlaceHolder; 
        RadGrid grid = cph.FindControl("RadGrid1"as RadGrid; 
 
        if(!IsPostBack) 
        { 
            DefineGridStructure(grid); 
        } 
 
        PopulateGrid(grid); 
    } 
 
    public override void DefineGridStructure(RadGrid grid) 
    { 
        grid.MasterTableView.DataKeyNames = new string[] { "CustomerID""Name"}; 
          
        GridBoundColumn boundColumn; 
        GridTemplateColumn templateColumn; 
         
        boundColumn = new GridBoundColumn(); 
        grid.MasterTableView.Columns.Add(boundColumn); 
        boundColumn.DataField = "Name"
        boundColumn.HeaderText = Resources.Strings.Name; 
 
        templateColumn = new GridTemplateColumn(); 
        grid.MasterTableView.Columns.Add(templateColumn); 
        templateColumn.DataField = "CustomerId"
        templateColumn.HeaderText = ""
        templateColumn.AllowFiltering = false
        templateColumn.ItemTemplate = new ImageLinkTemplate("CustomerId""~/Images/View.gif"
            "CustomersView.aspx", Resources.Strings.View); 
    } 
 
    public override void PopulateGrid(RadGrid grid) 
    { 
        CustomerService service = new CustomerService(); 
        grid.DataSource = service.GetGridData(base.GetCompanyId()); 
    } 
 

The problem I have is that whenever I do a post back (by sorting the grid or navigating to a different grid page), the GridTemplateColumn is no longer shown. Any suggestions on how to fix this?

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 06 Apr 2009, 10:33 AM
Hi Ion,

In the above given code you have added the Template column in the PageLoad event. I have found in the following help article that Column templates must be added in the Page_Init event handler, so that the template controls can be added to the ViewState. May be this is one of the reason why the template column is disappearing after PostBack.
Creating template columns programmatically

Shinu
0
Ion
Top achievements
Rank 1
answered on 06 Apr 2009, 11:57 AM
Shinu,
I've followed your advice about setting up GridTemplateColumns in Page_Init.
The problem now is that when I do a postback, the page seems to lock in loading mode (the Ajax 'loading' icon in the grid status bar keeps spinning, but the grid is not updated correctly. Also, when I generate a postback by interacting with the grid, Page_Load is not hit.
Here's the updated code:

protected void Page_Init(object sender, EventArgs e) 
    { 
        ContentPlaceHolder cph = Master.Master.FindControl("PageContents"as ContentPlaceHolder; 
        RadGrid grid = cph.FindControl("RadGrid1"as RadGrid; 
          
        GridTemplateColumn templateColumn; 
 
        templateColumn = new GridTemplateColumn(); 
        grid.MasterTableView.Columns.Add(templateColumn); 
        templateColumn.DataField = "CustomerId"
        templateColumn.HeaderText = ""
        templateColumn.AllowFiltering = false
        templateColumn.ItemTemplate = new ImageLinkTemplate("CustomerId""~/Images/View.gif"
            "CustomersView.aspx", Resources.Strings.View); 
    } 
 
    protected new void Page_Load(object sender, EventArgs e) 
    { 
        base.Page_Load(sender, e); 
        Master.Master.SetTitle(Resources.PageTitles.CustomersManager); 
 
        ContentPlaceHolder cph = Master.Master.FindControl("PageContents"as ContentPlaceHolder; 
        RadGrid grid = cph.FindControl("RadGrid1"as RadGrid; 
 
        if(!IsPostBack) 
        { 
            DefineGridStructure(grid); 
        } 
 
        PopulateGrid(grid); 
    } 
 
    public override void DefineGridStructure(RadGrid grid) 
    { 
        grid.MasterTableView.DataKeyNames = new string[] {"CustomerID"}; 
          
        GridBoundColumn boundColumn; 
        GridTemplateColumn templateColumn; 
         
        boundColumn = new GridBoundColumn(); 
        grid.MasterTableView.Columns.Add(boundColumn); 
        boundColumn.DataField = "Name"
        boundColumn.HeaderText = Resources.Strings.Name; 
 
    } 
 
    public override void PopulateGrid(RadGrid grid) 
    { 
        CustomerService service = new CustomerService(); 
        grid.DataSource = service.GetGridData(base.GetCompanyId()); 
    } 
0
Ion
Top achievements
Rank 1
answered on 07 Apr 2009, 06:09 AM
What I am actually trying to accomplish is to have one or more columns that contain Edit / Delete / View icons; when the users click on these icons, they should be redirected to the respective pages - E.G. Edit.aspx?Id=x, Delete.aspx?Id=x etc. (they should be using simple A tags or javascript document.location.href='', postback of the current page is not needed).

In case you don't have any suggestions for the problem in my post above, can you please suggest an alternative implementation for this that does GridTemplateColumns?
Tags
Grid
Asked by
Ion
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Ion
Top achievements
Rank 1
Share this question
or