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

Loading of user controls in Nested View Templates

2 Answers 233 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kishore Amireddy
Top achievements
Rank 1
Kishore Amireddy asked on 31 Jan 2011, 11:06 AM

Hi,

Plesae let us know how to achieve the following.

1. Avoid loading the user control in nested view template when outer grid/page loads.
2. Allow loading of user control ONLY for the expanded row of the outer grid.

Find below the source code to work on. Any prompt help on this is really appreciated.
Note: The source code really doesn't have any parent and child relation in the data that it displays because it just a sample.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="NVT._Default" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register Src="PlayerDetails.ascx" TagName="PlayerDetails" TagPrefix="Player" %>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="scriptManager" runat="server"></telerik:RadScriptManager>
        <telerik:RadGrid ID="grdPlayers" runat="server" OnNeedDataSource="GetPlayers" AutoGenerateColumns="true"
         OnItemCommand="grdPlayers_ItemCommand" OnItemCreated="grdPlayers_ItemCreated" >
            <MasterTableView>
                <NestedViewTemplate>
                    <asp:Panel runat="server" ID="InnerContainer" Visible="false">
                        <Player:PlayerDetails id="ucPlayerDetails" runat="server"></Player:PlayerDetails>
                    </asp:Panel>
                </NestedViewTemplate>
            </MasterTableView>
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
  
namespace NVT
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
  
        }
  
        protected void GetPlayers(object source, GridNeedDataSourceEventArgs e)
        {
            DataSet dsPlayers = new DataSet();
            DataTable tblPlayers = new DataTable();
            tblPlayers.Columns.Add("Name");
            tblPlayers.Columns.Add("Age");
  
            tblPlayers.Rows.Add("Sachin", "36");
            tblPlayers.Rows.Add("Virender", "30");
  
            dsPlayers.Tables.Add(tblPlayers);
  
            grdPlayers.DataSource = dsPlayers;
        }
  
        protected void grdPlayers_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.ExpandCollapseCommandName && e.Item is GridDataItem)
            {
                ((GridDataItem)e.Item).ChildItem.FindControl("InnerContainer").Visible =
                    !e.Item.Expanded;
            }
        }
  
        protected void grdPlayers_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridNestedViewItem)
            {
                e.Item.FindControl("InnerContainer").Visible = ((GridNestedViewItem)e.Item).ParentItem.Expanded;
            }
        }
    }
}
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PlayerDetails.ascx.cs" Inherits="NVT.PlayerDetails" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
  
<telerik:RadGrid ID="grdPlayerDetails" runat="server" AutoGenerateColumns="true">
</telerik:RadGrid>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
  
namespace NVT
{
    public partial class PlayerDetails : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetPlayerDetails();
            grdPlayerDetails.DataBind();
        }
  
        protected void GetPlayerDetails()
        {
            DataSet dsPlayerDetails = new DataSet();
            DataTable tblPlayerDetails = new DataTable();
            tblPlayerDetails.Columns.Add("Name");
            tblPlayerDetails.Columns.Add("Age");
  
            tblPlayerDetails.Rows.Add("Sachin", "36");
            tblPlayerDetails.Rows.Add("Virender", "30");
  
            dsPlayerDetails.Tables.Add(tblPlayerDetails);
  
            grdPlayerDetails.DataSource = dsPlayerDetails;
        }
    }
}

Thanks,
Kishore

2 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 03 Feb 2011, 12:15 PM
Hi Kishore,

Indeed, the suggested approach is to declare the user control in the NestedViewTemplate declaration and mark it as invisible if the parent item is not expanded.
However on ExpandCommand you can load the desired user control instead. In other words where you are nore making the Panel visible, you can instead call a method for loading the user control.

Kind regards,
Iana
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Ángel
Top achievements
Rank 1
answered on 22 Sep 2014, 03:28 PM
Hi,
I have the same scenario than  Kishore and I see that usercontrol's Page_Load is fired for each row even if the row is not expanded. It's necessary to add the ascx dynamically to prevent extra work? in the case of adding dynamically the ascx, can you provide a complete example?
Tags
Grid
Asked by
Kishore Amireddy
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Ángel
Top achievements
Rank 1
Share this question
or