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

How to use different templates for adding/editing nodes

3 Answers 163 Views
TreeList
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 07 Jul 2011, 04:45 PM
If I'm using FormTemplates to edit and insert records, is it possible to specify different FormTemplates depending on the selected node?

I'm trying to use the TreeList to display entities from various different tables.

As an example, let's say that I have 3 underlying SQL tables for Customer, Employee, Suppliers. Each table contains different fields.

I've created a top level stored procedure that provides  'grouping' rows and unions the data into the following structure.

NodeId, ParentNodeId, Firstname, PersonType

I can successfully bind this to my TreeList and the top level nodes allow me to drill down to Customer, Employee and Supplier. However, I need to be able to specify different form templates depending on the parent node.

Is it possible to do this?

Let me know if any of this is not clear.
Thanks,
Tim

3 Answers, 1 is accepted

Sort by
0
Tim
Top achievements
Rank 1
answered on 08 Jul 2011, 03:47 PM
I'm trying to see whether or not I can achieve this by using WebUserControls and writing code in some event to switch the control that is used.

I'm following these instructions.

http://www.telerik.com/help/aspnet-ajax/treelist-editform-types.html

When I click on the edit link, I get the following exception:

Cannot extract values from item when EditFormType is WebUserControl.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: Cannot extract values from item when EditFormType is WebUserControl.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NotSupportedException: Cannot extract values from item when EditFormType is WebUserControl.]
Telerik.Web.UI.TreeListEditFormItem.ExtractValues(IDictionary newValues) +304
Telerik.Web.UI.RadTreeList.CreateDataItems(IEnumerable`1 dataSource, ControlCollection tableRows) +1396
Telerik.Web.UI.RadTreeList.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +1053
System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +66
Telerik.Web.UI.RadTreeList.PerformDataBinding(IEnumerable data) +1459
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +128
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +33
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
Telerik.Web.UI.RadTreeList.DataBind() +94
Telerik.Web.UI.RadTreeList.AutoDataBind(TreeListRebindReason rebindReason) +200
Telerik.Web.UI.RadTreeList.Rebind() +39
Telerik.Web.UI.TreeListCommandEventArgs.ExecuteCommand(Object source) +452
Telerik.Web.UI.RadTreeList.OnBubbleEvent(Object source, EventArgs args) +200
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
Telerik.Web.UI.TreeListItem.OnBubbleEvent(Object source, EventArgs args) +113
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +125
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +169
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563


Here's my code:

<telerik:RadTreeList ID="RadTreeList1" runat="server" AllowMultiItemEdit="False"
    AutoGenerateColumns="False" Culture="(Default)" DataKeyNames="UniqueID" DataSourceID="dsTreeSQL"
    IsItemInserted="False" ParentDataKeyNames="ParentID" ShowOuterBorders="False">
    <EditFormSettings EditFormType="WebUserControl" UserControlPath="ctrlBusinessArea.ascx">
    </EditFormSettings>
    <Columns>
        <telerik:TreeListBoundColumn DataField="NodeText" HeaderText="Business Explorer"
            UniqueName="column">
            <HeaderStyle Font-Bold="True" />
        </telerik:TreeListBoundColumn>
        <telerik:TreeListEditCommandColumn>
        </telerik:TreeListEditCommandColumn>
    </Columns>
</telerik:RadTreeList>

The ASCX contains is pretty much empty.

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="ctrlBusinessArea.ascx.vb" Inherits="Content_Tools_ctrlBusinessArea" %>
 
Business Area

Can someone see what's wrong here?

Tim
0
Accepted
Veli
Telerik team
answered on 11 Jul 2011, 05:46 PM
Hello Tim,

I have reported the exception you are getting to the dev team and they will be looking at it. As a workaround to using a WebUserControl for this scenario, you need to implement the IBindableControl interface in your user control:

public partial class ctrlBusinessArea : System.Web.UI.UserControl, IBindableControl
{
    #region IBindableControl Members
 
    public void ExtractValues(System.Collections.Specialized.IOrderedDictionary dictionary)
    {
        //
    }
 
    #endregion
}

If you do not have any values to extract from the user control, simply leave the ExtractValues empty.

As for using different edit forms for different data items in RadTreeList, you can implement the scenario both with the FormTemplate and with a custom WebUserControl. In both cases, you need to define all your possible different edit forms. If you are using the FormTemplate feature, define different edit form content in different panels for example. Then, depending on the edited item, you can show some panels and hide others. You use the ItemDataBound event for that:

protected void RadTreeList1_ItemDataBound(object sender, TreeListItemDataBoundEventArgs e)
{
    if (e.Item is TreeListEditFormItem)
    {
        var editFormItem = (TreeListEditFormItem)e.Item;
        if (editFormItem.IsInEditMode)
        {
            //this is your parent data item. Use it to identify the type of edit form
            var dataItem = editFormItem.ParentItem;
            //then find controls in editFormItem.EditFormCell and hide/show them accordingly               
        }
    }
}

Alternatively, if you are using WebUserControls, you need to define 1 master user control to specify as RadTreeList's edit form. In your test case, for example, the ctrlBusinessArea can be your master user control. Then, either define different panels inside for the different edit forms (like in the above FormTemplate case), or define different nested user controls. You again show/hide them depending on the edited item. To get the RadTreeList edit form item from inside the user control, you can use the OnDataBinding overload:

protected override void OnDataBinding(EventArgs e)
{
    base.OnDataBinding(e);
 
    //this is your treelist edit form item. use it to identify the edit form to show
    var editFormItem = (TreeListEditFormItem)this.NamingContainer;
}

Greetings,
Veli
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Tim
Top achievements
Rank 1
answered on 13 Jul 2011, 10:37 AM
Hi Veli
Thanks for workaround
Tim
Tags
TreeList
Asked by
Tim
Top achievements
Rank 1
Answers by
Tim
Top achievements
Rank 1
Veli
Telerik team
Share this question
or