This is a migrated thread and some comments may be shown as answers.
Programatic GridTemplateColumns losing value on drilldown
3 Answers 63 Views
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 2
Stephen asked on 07 Apr 2009, 05:04 AM
I am implementing a drilldownable grid with two levels.  To maintain consistency and add some additional features I have subclassed the GridTemplateColumn and in the subclassed column, specify the templates to create the controls I wish to display.  This is working well.

However, when a drill down occurs and the grid performs a postback, the subclassed columns are always blank because no data-bind operation is ever called on them.  Is this a bug or is am I doing something wrong.  I wonder if the value is expected to be in the ViewState (hence no DataBind) but if so, where am I meant to persist the viewstate?

CodeBehind File

using

 

System;

 

 

 

using System.Collections.Generic;

 

 

 

using System.ComponentModel;

 

 

 

using System.Reflection;

 

 

 

using System.Web.UI;

 

 

 

using Telerik.Web.UI;

 

 


namespace 
GridColumns

 

 

{

 


#region
CodeBehind Class

 

  

 

public partial class _Default : System.Web.UI.Page

 

{

 

 

    protected void Page_Load(object sender, EventArgs e)

 

    {

    }

 

}

 

#endregion 

 

CodeBehind Class

 

 

 

 

#region Business Objects

 

 

[

DataObject(true)]

 

 

 

public class BusinessLogic

 

 

 

{

 

 

    public List<BusinessObject> GetList()

 

    {

 

        List<BusinessObject> list = new List<BusinessObject>();

 

        list.Add(

new BusinessObject() { ID = 1, Name = "A" });

 

        list.Add(

new BusinessObject() { ID = 2, Name = "B" }); 
        list.Add(
new BusinessObject() { ID = 3, Name = "C" });
        list.Add(
new BusinessObject() { ID = 4, Name = "D" });

 

 

        return list;

 

    }

 

 

    public List<ChildObject> GetChildren(int parentID)

 

 

    {

 

        List<ChildObject> list = new List<ChildObject>();

 

        list.Add(

new ChildObject() { ID = 100, Parent = parentID, Data = "QWE" });

 

        list.Add(

new ChildObject() { ID = 101, Parent = parentID, Data = "RTY" });

 

 

        return list;

 

    }

}

 

 

public class BusinessObject

 

{

 

 

    public int ID { get; set; }

 

 

    public string Name { get; set; }

 

}

 

 

public class ChildObject

 

{

 

 

    public int ID { get; set; }

 

 

    public int Parent { get; set; }

 

 

    public string Data { get; set; }

 

}

 

#endregion

 

Business Objects

 

 

 

#region 

 

TemplateColumn Classes

 

 

 

public class MyTemplateColumn : GridTemplateColumn

 

{

 

 

    public override void Initialize()

 

    {

 

        base.Initialize();

 

 

        this.ItemTemplate = new MyTemplate(this.DataField);

 

    }

}

 

 

public class MyTemplate : ITemplate

 

{

 

 

    private LiteralControl lControl;

 

 

    private string colname;

 

 

    public MyTemplate(string cName)

 

    {

        colname = cName;

    }

 

    public void InstantiateIn(System.Web.UI.Control container)

 

    {

        lControl =

new LiteralControl();

 

        lControl.ID =

"lControl";

 

        lControl.DataBinding +=

new EventHandler(lControl_DataBinding);

 

        container.Controls.Add(lControl);
    }

 

    public void lControl_DataBinding(object sender, EventArgs e)

 

    {

 

        LiteralControl l = (LiteralControl)sender;

 

 

        GridDataItem container = (GridDataItem)l.NamingContainer;

 

 

        PropertyInfo property = typeof(BusinessObject).GetProperty(colname);

 

        l.Text = property.GetValue(container.DataItem,

null).ToString();

 

    }

}

 

#endregion 

 

TemplateColumn Classes

 

 

}

ASPX File
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridColumns._Default" %>

 

<%

@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>

 

<%

@ Register assembly="GridColumns" namespace="GridColumns" tagprefix="me" %>

 

     

<!

 

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<

 

html xmlns="http://www.w3.org/1999/xhtml" >

 

<

 

head runat="server">

 

 

 

<title></title>

 

</

 

head>

 

<

 

body>

 

<form id="form1" runat="server">

 

 

    <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager>

 

 

 

    <div>

 

    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="ObjectDataSource1" 
                    GridLines="None" AutoGenerateColumns="False">

 
        <

 

MasterTableView CellSpacing="-1" DataKeyNames="ID" DataSourceID="ObjectDataSource1">

 

 

 

 

 

            <DetailTables>

 

 

                <telerik:GridTableView runat="server" CellSpacing="-1" DataKeyNames="ID" DataSourceID="ObjectDataSource2">

 

 

 

                <ParentTableRelation>
                    
<telerik:GridRelationFields DetailKeyField="ParentID" MasterKeyField="ID" />
                
</ParentTableRelation>

 

 

 

               <Columns>

 

 

 

                    <telerik:GridBoundColumn DataField="ID" DataType="System.Int32" HeaderText="ID" UniqueName="ID">
                    </telerik:GridBoundColumn>

 

 

 

                    <telerik:GridBoundColumn DataField="Data" HeaderText="Data" UniqueName="Data"> 

 

                    </telerik:GridBoundColumn>

 

 

                </Columns>

 

 

 

                <RowIndicatorColumn><HeaderStyle Width="20px" /></RowIndicatorColumn>

 

 

 

                <ExpandCollapseColumn><HeaderStyle Width="20px" /></ExpandCollapseColumn>

 

 

 

            </telerik:GridTableView>

 

 

 

        </DetailTables>

 

        <

 

RowIndicatorColumn><HeaderStyle Width="20px"></HeaderStyle></RowIndicatorColumn>

 

        <

 

ExpandCollapseColumn Visible="True"><HeaderStyle Width="20px"></HeaderStyle></ExpandCollapseColumn>
        <Columns>

 

 

 

            <telerik:GridBoundColumn DataField="ID" DataType="System.Int32" HeaderText="ID" UniqueName="ID">

 

 

 

            </telerik:GridBoundColumn>

 

 

 


            <!-- Standard Template Column -->

 

            <telerik:GridTemplateColumn DataField="Name" HeaderText="Name" UniqueName="Name">

 

                <EditItemTemplate>

 

 

                    <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>

 

 

 

                </EditItemTemplate>

 

 

 

                <ItemTemplate>

 

 

 

                    <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>'></asp:Label>

 

 

 

                </ItemTemplate>

 

             

 

</telerik:GridTemplateColumn>

 

 

             

<!-- Subclassed Template Column -->

 

 

            <me:MyTemplateColumn DataField="Name" HeaderText="Name" UniqueName="Name"> 

 

            </me:MyTemplateColumn>

 

 

 

 

 

        </Columns>

    </

 

MasterTableView>

 

 

 

</telerik:RadGrid>

 

 

 

 

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetList" 
            TypeName="GridColumns.BusinessLogic">
</
asp:ObjectDataSource>

 

 

 

 

<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetChildren" 
            
TypeName="GridColumns.BusinessLogic">

 

 

 

    <SelectParameters>

 

 

 

        <asp:Parameter Name="parentID" Type="Int32" />

 

 

 

    </SelectParameters>

 

 

 

</asp:ObjectDataSource>

 

 

</div>

 

 

</form>

 

</

 

body>

 

</

 

html>

Thanks

 

 

 

 

3 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 09 Apr 2009, 04:27 PM
Hello Stephen,

I've modified the code which you posted and created sample application which I believe resolves the issue in question.
You can find the application attached to this post.

Few things you should have in mind:
 - when you inherit any GridColumn do not forget to override Clone method as explained on the following help article:
Inheriting grid columns
 - you can use DataBinder.Eval to extract value from DataItem

I hope this helps.

Best wishes,
Nikolay
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Stephen
Top achievements
Rank 2
answered on 12 Apr 2009, 03:57 AM
Hi Nikolay

Thanks for that but the problem remains.  In the code you posted, the second Name field on the first level (using the inherited column) disappears when the drilldown occurs.  In the overridden Initialise method, the ItemTemplate is set, but when the clone is called the value of ItemTemplate has not been set.  Does this mean that on postback the clone is called before the Initialise?

On your version, does the second name field go blank on drilldown?  I am using 2009 Q1.

Should the ItemTemplate be set earlier (constructor?) or is there something else going on here?

Thanks in advance

Stephen
0
Nikolay Rusev
Telerik team
answered on 14 Apr 2009, 12:24 PM
Hello Stephen,

"..second Name..disappears .." - you are right about this is due to the fact that we are using LiteralControl in ItemTemplate which does not save it's Text in ViewState. If you replace LiteralControl with Label all the values will be persisted on postback.

Please give it a try.

Greetings,
Nikolay
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Stephen
Top achievements
Rank 2
Answers by
Nikolay Rusev
Telerik team
Stephen
Top achievements
Rank 2
Share this question
or