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

Selfhierarchy: "No child records to display" in user controls

1 Answer 87 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nick
Top achievements
Rank 1
Nick asked on 12 Jul 2010, 02:06 PM
Hello everybody.
I have followed the self hierarchy example you published under the ASP.NET AJAX controls page.
As long as I use it in a ASPX page everyhting works fine, but I unfortunately need to use it in an user control.
Since the function " HideExpandColumnRecursive" is called by trapping the Page_PrerenderCompletedEvent and this event does not exists in user controls, how can I hide the rows displaying "No child records to display"?
I mean, is there any event to trap in the user control or any other way to do that?

The following is the ASCX code:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ForumSummary.ascx.cs" Inherits="RadControlsWebApp.ForumSummaryControl" %>
    <body>
        <telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server" />
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
            <Scripts>
                <%--Needed for JavaScript IntelliSense in VS2010--%>
                <%--For VS2008 replace RadScriptManager with ScriptManager--%>
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
            </Scripts>
        </telerik:RadScriptManager>
        <script type="text/javascript">
        </script>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadSkinManager ID="RadSkinManager1" runat="server" Skin="WebBlue">
        </telerik:RadSkinManager>
        <div>
            <asp:Label ID="Label1" runat="server" 
                        Text="Forum summary" 
                        ForeColor="White" 
                        BackColor="#9900FF"
                        BorderColor="#000099" 
                        BorderStyle="Solid" 
                        Font-Names="Arial" 
                        Font-Size="11pt" />
        </div>
        <div>
            <telerik:RadGrid ID="radGridParent" 
                                runat="server" 
                                GridLines="None"
                                Height="500px" ShowHeader="False"
                                Width="1000px"
                                Skin="Windows7"
                                AutoGenerateColumns="False"
                                OnColumnCreated="radGridParent_ColumnCreated"
                                OnItemCreated="radGridParent_ItemCreated"
                                OnItemDataBound="radGridParent_ItemDataBound">
                <ClientSettings>
                    <Selecting AllowRowSelect="True" />
                    <Scrolling AllowScroll="True" UseStaticHeaders="True" />               
                    </ClientSettings>
                <MasterTableView DataKeyNames="UniqueID, ParentID" 
                                    ShowHeader="True" 
                                    ShowFooter="true"
                                    AllowPaging="False" 
                                    PageSize="10"
                                    EditMode="InPlace"
                                    HierarchyDefaultExpanded="true" 
                                    ExpandCollapseColumn-Visible="false" 
                                    ShowHeadersWhenNoRecords="false"
                                    HierarchyLoadMode="Client" 
                                    CommandItemDisplay="None">
                    <SelfHierarchySettings ParentKeyName="ParentID" KeyName="UniqueID" />
                    <Columns>
                        <telerik:GridTemplateColumn UniqueName="columnUniqueID" Visible="False">
                            <ItemTemplate>
                                <asp:Label ID="lblUniqueID" runat="server" Text='<%# Eval("CategoryRef.UniqueID") %>' />
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn UniqueName="columnParentID" Visible="false">
                            <ItemTemplate>
                                <asp:Label ID="lblParentID" runat="server" Text='<%# Eval("CategoryRef.ParentID") %>' />
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn UniqueName="columnName">
                            <HeaderStyle HorizontalAlign="Center" Width="370px"/>
                            <HeaderTemplate>
                                <asp:Label ID="lblNameHeader" runat="server" Text="Categoria"/>                                   
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:LinkButton ID="hLinkName" runat="server" OnClick="hLinkName_Click" Text='<%# Eval("CategoryRef.Name") %>'/>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="textBoxName" runat="server" Text='<%# Eval("CategoryRef.Name") %>'/>
                                <asp:DropDownList ID="dropDownListParentsEdit" runat="server" DataValueField="UniqueID" DataTextField="Name" />
                            </EditItemTemplate>
                            <FooterTemplate >
                                <asp:TextBox ID="textBoxInsertName" runat="server"/>
                                <asp:DropDownList ID="dropDownListParents" runat="server" DataValueField="UniqueID" DataTextField="Name" />
                            </FooterTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn UniqueName="columnDiscussionsNumber">
                            <HeaderStyle HorizontalAlign="Center" Width="100px"/>
                            <HeaderTemplate>
                                <asp:Label ID="lblDiscussionsNumberHeader" runat="server" Text="Discussioni"/>                                   
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblDiscussionsNumber" runat="server" Text='<%# Eval("DiscussionsNumber") %>' />
                            </ItemTemplate>
                            <FooterTemplate >
                                <asp:Button ID="buttonInsert" runat="server" Text="Crea" OnClick="buttonInsert_Click"/>
                            </FooterTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn UniqueName="columnPostsNumber">
                            <HeaderStyle HorizontalAlign="Center" Width="100px"/>
                            <HeaderTemplate>
                                <asp:Label ID="lblPostsNumberHeader" runat="server" Text="Messaggi"/>                                   
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblPostsNumber" runat="server" Text='<%# Eval("PostsNumber") %>' />
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn UniqueName="columnLastPostUrl">
                            <HeaderStyle HorizontalAlign="Center" Width="300px"/>
                            <HeaderTemplate>
                                <asp:Label ID="lblLastPostUrlHeader" runat="server" Text="Ultimo messaggio"/>                                   
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:HyperLink ID="hLinkLastPostUrl" runat="server" NavigateUrl='<%# Eval("LastPostUrl") %>' Text='<%# Eval("LastPostUrl") %>'/>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridEditCommandColumn>
                        </telerik:GridEditCommandColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>
        </div>
    </body>
</html>

And this the code behind:
protected void Page_Load(object sender, EventArgs e)
{
    if (Assembly.GetAssembly(typeof(ScriptManager)).FullName.IndexOf("3.5") != -1 || Assembly.GetAssembly(typeof(ScriptManager)).FullName.IndexOf("4.0") != 1)
    {
        radGridParent.MasterTableView.FilterExpression = @"!it.ParentID.HasValue";
    }
    else
    {
        radGridParent.MasterTableView.FilterExpression = "ParentID IS NULL";
    }
    LoadData();
    radGridParent.DataSource = forumSummaries;
}
protected void radGridParent_ItemCreated(object sender, GridItemEventArgs e)
{
    CreateExpandCollapseButton(e.Item, "columnName");
    if (e.Item is GridHeaderItem && e.Item.OwnerTableView != radGridParent.MasterTableView)
    {
        e.Item.Style["display"] = "none";
    }
    if (e.Item is GridFooterItem)
    {
        DropDownList ddlList = e.Item.FindControl("dropDownListParents") as DropDownList;
        if (ddlList != null)
        {
            if (ddlList.DataSource == null)
            {
                ddlList.DataSource = GetCategoriesForDropDownList();
            }
        }
    }
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        ForumSummary summary = e.Item.DataItem as ForumSummary;
        GridEditableItem item = e.Item as GridEditableItem;
        DropDownList list = item.FindControl("dropDownListParentsEdit") as DropDownList;
        if (list != null)
        {
            list.DataSource = GetCategoriesForDropDownList();
            if (summary != null)
            {
                if (summary.CategoryRef.ParentID != null)
                {
                    list.SelectedValue = summary.CategoryRef.ParentID.ToString();
                }
            }
        }
    }
    if (e.Item is GridNestedViewItem)
    {
        e.Item.Cells[0].Visible = false;
    }
}
public void HideExpandColumnRecursive(GridTableView tableView)
{
    GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView);
    foreach (GridNestedViewItem nestedViewItem in nestedViewItems)
    {
        foreach (GridTableView nestedView in nestedViewItem.NestedTableViews)
        {
            nestedView.Style["border"] = "0";
            Button MyExpandCollapseButton = (Button)nestedView.ParentItem.FindControl("MyExpandCollapseButton");
            if (nestedView.Items.Count == 0)
            {
                if (MyExpandCollapseButton != null)
                {
                    MyExpandCollapseButton.Style["visibility"] = "hidden";
                }
                nestedViewItem.Visible = false;
            }
            else
            {
                if (MyExpandCollapseButton != null)
                {
                    MyExpandCollapseButton.Style.Remove("visibility");
                }
            }
            if (nestedView.HasDetailTables)
            {
                HideExpandColumnRecursive(nestedView);
            }
        }
    }
}
public void CreateExpandCollapseButton(GridItem item, string columnUniqueName)
{
    if (item is GridDataItem)
    {
        int level = item.ItemIndexHierarchical.Split(':').Length;
        if (level > 1)
        {
            foreach (WebControl ctr in item.Controls)
            {
                for (int i = 0; i < ctr.Controls.Count; i++)
                {
                    if (ctr.Controls[i] is WebControl && !(ctr.Controls[i] is Button))
                    {
                        (ctr.Controls[i] as WebControl).Style["margin-left"] = level * 10 + "px";
                    }
                }
            }
        }
    }
}
As you can see by reading the code behind it misses a place where to call the HideExpandColumnRecursive that hides the the sub row of empty childs.
Is there any way to hide that??
Thanks a lot in advance.
Nick

1 Answer, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 12 Jul 2010, 03:45 PM
Hello Nick,

You can wire the Page_PreRenderComplete event of the page, and call the HideExpandColumnRecursive() method from its handler. You will also need to get a reference to the grid using the FindControl() method of your user control. I have attached a small sample based on our online example that demonstrates this approach.

I hope this helps

Regards,
Martin
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
Grid
Asked by
Nick
Top achievements
Rank 1
Answers by
Martin
Telerik team
Share this question
or