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

Using WebUserControl as nested view template

3 Answers 152 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Al
Top achievements
Rank 1
Iron
Iron
Iron
Al asked on 13 Aug 2010, 11:36 AM
Hi
I am creating my grid (with nested view) fully in code-behind & it all works fine when I set 'NestedViewTemplate' to my custom template class. What I was wondering is whether I could use an ascx instead of a ITemplate class?

Out of interest I took a long shot and tried this but it doesn't work:

 

 

public class ChildTable :ITemplate

 

{

 

 

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

 

    {

 

 

        Table tb = new Table();

 

 

 

        TableRow tr = new TableRow();

 

 

 

        TableCell td = new TableCell();

 

 

 

        TextBox txt = new TextBox();

 

 

 

        MyUserControl uc = new MyUserControl();

 

        td.Controls.Add(uc);

        tr.Cells.Add(td);

        tb.Rows.Add(tr);

        container.Controls.Add(tb);

    }

}

3 Answers, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 13 Aug 2010, 12:41 PM
Hi Al,

You cannot use an ascx instead of ITemplate class because the NestedViewTemplate requires an object inheriting ITemplate.

Also, could you please elaborate a little more on the scenario where you add the user control in the template class? How are you binding the RadGrid control? When do you create the NestedViewTemplate (it must be done in the Page_Init event handler)? Do you recieve an error or the NestedViewTemplate just does not create/bind properly?

Greetings,
Tsvetina
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
0
Al
Top achievements
Rank 1
Iron
Iron
Iron
answered on 13 Aug 2010, 12:54 PM
Thanks Tsvetina, my nestedviewtemplate is created in Page_Init, RadGrid is bound to a Linq datasource in the 'NeedDataSource event' & my usercontrol is very simple ie. a textbox and a button, just for testing. I get no errors, the row expands as expected but the place where I would expect to see my usercontrol is just empty.

The real problem I am facing is creating a newstedviewtemplate in code that contains a detail grid (or grids) as I can't see any examples of how to do that, could you perhaps point me in the right direction?
0
Al
Top achievements
Rank 1
Iron
Iron
Iron
answered on 16 Aug 2010, 08:23 AM
Ok, I have managed to get a nested view template with a detail grid working from purely code-behind. There are some tricks that I have found mentioned on other posts so I thought I'd share them as I didn't see an example listing all of these together:

 - Create a grid in code-behind

 - Create the detail grid template class inheriting ITemplate and assign it to MainGrid.MasterTableView.NestedViewTemplate

 - The main grid will have it's Datasource (in mycase a LINQ-EF query) set in 'NeedDataSource' as per normal

 - Consume 'MainGrid_ItemCreated' to populate the detail table, then setup the detail grid datahandler:

        protected void MainGrid_ItemCreated(object sender, GridItemEventArgs e) 
        {
            if (e.Item is GridNestedViewItem) 
            { 
                //var Irn = (int)((GridDataItem)e.Item).GetDataKeyValue("SomeField"); 
                RadGrid rgTemp = e.Item.FindControl("RadGrid2") as RadGrid; 
                rgTemp.NeedDataSource += new GridNeedDataSourceEventHandler(_Child_NeedDataSource); 
             }
        }

 - '_Child_NeedDataSource' looks like this:

            protected void _Child_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
            { 
                // ((source as RadGrid).NamingContainer as GridNestedViewItem) gives a reference to the parent nested view item 

                GridNestedViewItem nestedItem = ((source as RadGrid).NamingContainer as GridNestedViewItem); 
                int MyPK = (int)nestedItem.ParentItem.GetDataKeyValue("MyPK"); 

                var x = from i in Entity.Table 
                    where i.SomePK == MyPK
                    select new { A = i.A, B = i.B}; 
        
                (source as RadGrid).DataSource = x; 

            }

 - At this point the main/child grids work fine, except that the event '_Child_NeedDataSource' will not fire. This is because the
   detail grid is initially not visible when it is expanded and '_NeedDataSource' doesn't fire for invisible grids.
   For this we need to call Rebind in 'ItemCommand' of the main grid to force the inital bind:

        void MainGrid_ItemCommand(object source, GridCommandEventArgs e) 
        { 
            if (e.CommandName == RadGrid.ExpandCollapseCommandName) 
            { 
                GridDataItem item = e.Item as GridDataItem; 
                if (!item.Expanded) 
                { 
                    GridNestedViewItem nestedItem = (GridNestedViewItem)item.ChildItem; 
                    RadGrid tempGrid = (RadGrid)nestedItem.FindControl("RadGrid2"); 
                    tempGrid.Rebind(); 
                } 
            } 
        }

 - That's it, the main and child grid should work just fine now
Tags
Grid
Asked by
Al
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Tsvetina
Telerik team
Al
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or