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

[Solved] Problem with Templated column on Postback

1 Answer 105 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Adrian
Top achievements
Rank 1
Adrian asked on 12 Feb 2010, 04:30 PM
Hi There,

I have a Grid control with a column which is used to render an icon based on a bound value.

The DocumentIconTemplate class looks like this

 

 

Public Class DocumentIconTemplate  
    Implements ITemplate  
 
    Private m_Name As String 
 
    Public Sub New(ByVal name As String)  
        m_Name = name  
    End Sub 
 
 
    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn  
 
        Dim dataCell As GridTableCell = container  
        Dim dataItem As GridDataItem = CType(container.NamingContainer, GridDataItem)  
 
        Dim img As New Image()  
        img.ID = m_Name  
        AddHandler img.DataBinding, AddressOf BindImage  
 
        container.Controls.Add(img)  
 
    End Sub 
 
    Public Sub BindImage(ByVal sender As ObjectByVal e As EventArgs)  
        Dim img As Image = CType(sender, Image)  
        Dim container As GridDataItem = CType(img.NamingContainer, GridDataItem)  
        img.ID = m_Name  
        img.ImageUrl = "Scr.Bmp" 
    End Sub 
 
 
End Class 

and the custom column looks like this
            Dim column As GridTemplateColumn  
 
            column = New GridTemplateColumn()  
            column.HeaderText = "Type" 
            column.DataField = "Type" 
            column.UniqueName = "Type" 
            column.ItemTemplate = New DocumentIconTemplate("Type")  
            gridControl.MasterTableView.Columns.Add(column) 

All is well and this renders fine until I create a hierarchical grid or use some operation that requires a postback. When I expand the row (to show the second level of the hierarchy) my custom image disappears although all "text" based fields remain.When you expand the row the grid posts back to the server and when it comes back Grid columns that have Text set on them still have their values. I'm not sure if it the viewstate or the control heirarchy that is the problem here. On postback the grid doesn't call the InstantiateIn again so I presume it's trying to re-render itself just using viewstate.

It does keep the image if you use the <ItemTemplate> and create the image declaratively however outside of this simple test app the columns need to be created dynamically.

Any help/thoughts appreciated.

Adrian

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 17 Feb 2010, 01:07 PM
Hello Adrian,

As you guessed, RadGrid restore its control data from the ViewState on successive postbacks when the gridi is not rebound. However, note that your DocumentIconTemplate class is not saving anything in the ViewState. So it cannot restore any data. Additionally, I see that you are setting your image properties in the DataBinding event of the Image control. This event, however, is not fired on successive postbacks if your grid is not rebound.

Additionally, if you are dynamically creating the columns of a statically declared RadGrid, note that you need to add the columns to the MasterTableView.Columns collection first, before setting their values:

Dim column As GridTemplateColumn 
 
column = New GridTemplateColumn() 
gridControl.MasterTableView.Columns.Add(column)
column.HeaderText = "Type"
column.DataField = "Type"
column.UniqueName = "Type"
column.ItemTemplate = New DocumentIconTemplate("Type"


Best wishes,
Veli
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
General Discussions
Asked by
Adrian
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or