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

Hierarchical RadGrid Inside Dynamic RadDock

2 Answers 98 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Thiyagu
Top achievements
Rank 1
Thiyagu asked on 18 Nov 2011, 09:03 PM
//Master Page Code Behind
 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
}
 
//Web Page Code Behind
 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Telerik.Web.UI;
 
public partial class HierarchyRadGrd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
 
        }
 
        myHomeContent.Controls.Add(GetBandRadDock(2, 3, "Plan Details"));
         
 
        if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
        {
            string controlPath = string.Empty;
            string sourceid = "";
            try
            {
                if (ScriptManager.GetCurrent(this).AsyncPostBackSourceElementID.Contains("$"))
                    sourceid = ScriptManager.GetCurrent(this).AsyncPostBackSourceElementID.Split('$')[2];
                else
                    sourceid = ScriptManager.GetCurrent(this).AsyncPostBackSourceElementID.Split('_')[2];
                controlPath = "UserControlRadGrid.ascx";
                LoadUserControl(sourceid);
            }
            catch (IndexOutOfRangeException ExoutOf)
            {
            }
        }
 
 
    }
 
    protected void LoadUserControl(string _BandId)
    {
        string SourceString = "";
        string updateControl = "";
        if (ScriptManager.GetCurrent(this).AsyncPostBackSourceElementID.Contains("$"))
            SourceString = ScriptManager.GetCurrent(this).AsyncPostBackSourceElementID;   
        else
            SourceString = ScriptManager.GetCurrent(this).AsyncPostBackSourceElementID;
        string IdVal = SourceString;
        string ajpCtrl = "UpdatePanel3";
        string _sActualPath = SourceString;
        AddControl(_sActualPath, ajpCtrl, "UserControlRadGrid.ascx", _BandId);
    }
 
    protected void AddControl(string sourcedock, string ajaxp, string path, string _sBandId)
    {
        RadDock SourceDock = this.FindControl(sourcedock) as RadDock;
 
        UpdatePanel SourcePanel = (UpdatePanel)SourceDock.ContentContainer.FindControl(ajaxp);
 
        SourcePanel.ContentTemplateContainer.Controls.Clear();
        UserControl mycon = (UserControl)LoadControl(path);
        mycon.ID = "DC" + _sBandId;
 
        switch (_sBandId)
        {
 
            case "3":
                ((UserControlRadGrid)mycon)._ShowBand = false;
                break;
 
        }
 
        SourcePanel.ContentTemplateContainer.Controls.Add(mycon);
 
 
    }
 
    private RadDock GetBandRadDock(int Sequence, int _iBandId, string _sBandTitle)
    {
        RadDock _radDock = new RadDock();
        _radDock.CommandsAutoPostBack = true;
        _radDock.DockMode = DockMode.Docked;
        _radDock.DefaultCommands = Telerik.Web.UI.Dock.DefaultCommands.ExpandCollapse;
        _radDock.Command += new DockCommandEventHandler(_radDock_Command);
        _radDock.OnClientCommand = "OnDockExpand";
        _radDock.ForbiddenZones = new string[] { "DockZone" };
        _radDock.DockMode = DockMode.Default;
        _radDock.Collapsed = true;
        _radDock.ExpandText = "Loading...";
        _radDock.EnableAnimation = true;
        _radDock.Title = _sBandTitle;
        _radDock.Font.Bold = true;
        _radDock.ID = _iBandId.ToString();
        UpdatePanel uPanel = new UpdatePanel();
        uPanel.ID = "UpdatePanel" + _iBandId.ToString();
        uPanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
        uPanel.RenderMode = UpdatePanelRenderMode.Inline;
        AsyncPostBackTrigger oTrigger = new AsyncPostBackTrigger();
        oTrigger.ControlID = _radDock.ID;
        uPanel.Triggers.Add(oTrigger);
        Label oLabel = new Label();
        oLabel.Text = "Loading....";
        oLabel.ID = "ldng";
        uPanel.ContentTemplateContainer.Controls.Add(oLabel);
        _radDock.ContentContainer.Controls.Add(uPanel);
        return _radDock;
 
    }
 
    void _radDock_Command(object sender, DockCommandEventArgs e)
    {
        Control _DynamicControl = new Control();
        RadDock oDock = new RadDock();
 
        oDock = sender as RadDock;
        oDock.AutoPostBack = false;
        oDock.CommandsAutoPostBack = false;
 
        UpdatePanel uPanel = new UpdatePanel();
        string uId = "UpdatePanel" + oDock.ID;
        uPanel = (UpdatePanel)oDock.ContentContainer.FindControl(uId);
        uPanel.ContentTemplateContainer.Controls.Clear();
        int _iBandId = Convert.ToInt32(oDock.ID);
 
        if (oDock.Collapsed)
        {
            Label oLabel = new Label();
            oLabel.Text = "Loading....";
            uPanel.Controls.Clear();
            uPanel.Controls.Add(oLabel);
        }
        else
        {
            switch (_iBandId)
            {               
                case 3:
 
                    _DynamicControl = LoadControl("UserControlRadGrid.ascx");
                    _DynamicControl.ID = "DC3";
                    _DynamicControl.GetType().GetProperty("ShowBand").SetValue(_DynamicControl, false, null);
                    break;              
            }
            uPanel.ContentTemplateContainer.Controls.Add(_DynamicControl);
        }
    }
 
 
 
    public void RaisePostBackEvent(string eventArgument) { }
}
 
//User Control Code Behind
 
 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Telerik.Web.UI;
 
public partial class UserControlRadGrid : System.Web.UI.UserControl
{
 
 
    private SqlConnection _con;
    private SqlDataAdapter _adp;
    private static DataTable _dt;
    public bool _ShowBand = true;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RadGrid1.DataSource = BindData();
            RadGrid1.DataBind();
        }
 
        RadGrid1.DataSource = BindData();
        RadGrid1.DataBind();
 
    }
 
    public DataTable BindData()
    {
        try
        {
            _con = new SqlConnection(ConfigurationManager.ConnectionStrings["ShopConnectionString"].ConnectionString);
            _adp = new SqlDataAdapter("SELECT * FROM Shop", _con);
            _dt = new DataTable();
            _adp.Fill(_dt);
        }
        catch (Exception e)
        {
            Response.Write(e.Message.ToString());
        }
        return _dt;
    }
 
    protected void RadGrid1_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)
    {
 
        GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
 
        switch (e.DetailTableView.Name)
        {
            case "Category":
                {
                    string _shopId = dataItem.GetDataKeyValue("ShopID").ToString();
                    e.DetailTableView.DataSource = GetNextLevelCategory(_shopId);
                    break;
                }
            case "Product":
                {
                    string _categoryID = dataItem.GetDataKeyValue("CategoryID").ToString();
                    e.DetailTableView.DataSource = GetNextLevelProduct(_categoryID);
                    break;
                }
        }
    }
 
    protected DataTable GetNextLevelCategory(string _shopId)
    {
        try
        {
            _con = new SqlConnection(ConfigurationManager.ConnectionStrings["ShopConnectionString"].ConnectionString);
            _adp = new SqlDataAdapter("SELECT * FROM Category WHERE ShopId=" + _shopId, _con);
            _dt = new DataTable();
            _adp.Fill(_dt);
        }
        catch (Exception e)
        {
            Response.Write(e.Message.ToString());
        }
        return _dt;
    }
 
    protected DataTable GetNextLevelProduct(string _categoryID)
    {
        try
        {
            _con = new SqlConnection(ConfigurationManager.ConnectionStrings["ShopConnectionString"].ConnectionString);
            _adp = new SqlDataAdapter("SELECT * FROM Products WHERE CategoryId=" + _categoryID, _con);
            _dt = new DataTable();
            _adp.Fill(_dt);
        }
        catch (Exception e)
        {
            Response.Write(e.Message.ToString());
        }
        return _dt;
    }
}





<!--Master Page-->
 
 
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" EnableScriptGlobalization="true"
    AsyncPostBackTimeout="600" ScriptMode="Release"/>       
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
         
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>
 
<!--Web Page-->
 
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="HierarchyRadGrd.aspx.cs" Inherits="HierarchyRadGrd" Title="Untitled Page" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register Src="UserControlRadGrid.ascx" TagName="PlanDetailsUCSample" TagPrefix="control" %>
 
 
 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
 
 
<script type="text/javascript">
 
function OnDockExpand(sender,args)
{
        if(sender._collapsed == true)
        {
            args._cancel = true;
        }
        else
        {
            if(navigator.appName != "Netscape")
            {
                if(sender._contentContainer.childNodes[0].childNodes.length > 1)
                    args.set_cancel(true);  
            }
            else
            {
                if(sender._contentContainer.children[0].children.length > 1)
                   args.set_cancel(true);  
            }
        }
}
 
</script>
 
 
 
 
<telerik:RadDockLayout ID="dckLayout" runat="server">
    <telerik:RadDockZone ID="myHomeContent" runat="server" FitDocks="true" Font-Bold="true"
        BorderStyle="None" Orientation="Vertical">
    </telerik:RadDockZone>
</telerik:RadDockLayout>
    <center>
        <table cellpadding="0" cellspacing="0" width="97%">
            <tr>
                <td>
                    <control:plandetailsucsample id="PlanDetailsUCSample" runat="server" />
                </td>
            </tr>
        </table>
    </center>
    <br />
    <br />                            
</asp:Content>
 
<!--User Control-->
 
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserControlRadGrid.ascx.cs" Inherits="UserControlRadGrid" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<%--<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadGrid1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>--%>
 
<telerik:RadGrid ID="RadGrid1" runat="server" Width="60%" AutoGenerateColumns="false"
    OnDetailTableDataBind="RadGrid1_DetailTableDataBind" RegisterWithScriptManager="true">
    <MasterTableView DataKeyNames="ShopID">
        <Columns>
            <telerik:GridBoundColumn HeaderText="ShopID" DataField="ShopID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="ShopName" DataField="ShopName">
            </telerik:GridBoundColumn>
        </Columns>
        <DetailTables>
            <telerik:GridTableView DataKeyNames="CategoryID" Name="Category">
                <Columns>
                    <telerik:GridBoundColumn HeaderText="CategoryID" DataField="CategoryID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="CategoryName" DataField="CategoryName">
                    </telerik:GridBoundColumn>
                </Columns>
                <DetailTables>
                    <telerik:GridTableView Name="Product">
                        <Columns>
                            <telerik:GridBoundColumn HeaderText="ProductID" DataField="ProductID">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText="ProductName" DataField="ProductName">
                            </telerik:GridBoundColumn>
                        </Columns>
                    </telerik:GridTableView>
                </DetailTables>
            </telerik:GridTableView>
        </DetailTables>
    </MasterTableView>
</telerik:RadGrid>

 Hi,


I am loading a Hierarchical RadGrid inside a dynamically created RadDock, and the RadGrid is inside a user control. So when the RadDock is expanded the usercontrol(with the RadGrid) is loaded. And on expanding the Hierarchical RadGrid the next level of items is not shown. This is because of ispostback property which is always true for the usercontrol. And so the RadGrid inside the usercontrol is creating problem. It is working fine for a plain grid without any levels, but i am in need of using a Hierarchical RadGrid. Would appreciate if any help is provided for this. I want to know is that scenario possible or not and the way for doing it if possible. Thank you. Attaching the code below. 

2 Answers, 1 is accepted

Sort by
0
Maria Ilieva
Telerik team
answered on 23 Nov 2011, 11:01 AM
Hi Thiyagu,

Could you please ensure that the ViewState of the page is not disabled as this may cause the described behaviour. Also please note that the user controls should not be recreated overall on each postback and it will recreate them in their initial state and on each post back as you mentioned you will not be able to perform the needed action. Please review the following help topic which elaborates on loading UserControls.
Also please debug your application locally and verify if the DetailTableDataBind event fires correctly.


Kind regards,
Maria Ilieva
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Thiyagu
Top achievements
Rank 1
answered on 25 Nov 2011, 03:50 PM
Hi Maria,


Thanks for the information. I solved the issue by adding hierarchy load mode property and setting it to client. Once again thanks.
Tags
Grid
Asked by
Thiyagu
Top achievements
Rank 1
Answers by
Maria Ilieva
Telerik team
Thiyagu
Top achievements
Rank 1
Share this question
or