Access FormTemplate Controls from ItemCommand

7 posts, 2 answers
  1. Jon
    Jon avatar
    813 posts
    Member since:
    Oct 2008

    Posted 06 Dec 2011 Link to this post

    Hi,

    I have  a RadTreeList with a FormTemplate.  With the custom form I have a RadComboBox that in turn has a RadTreeView inside it to allow me to assign the hierarchical data to the record that is being edited.  

    I need to be able to access the RadComboBox on the FormTemplate from the ItemCommand event so that I can set it up for the node being edited.  

    Below is where I am at, I can get the text and ID of the parent node (if there is one).  Now I need to assign to the RadComboBox and RadTreeView on the EditForm.  I'm fine doing that if only I could find the controls themselves.

    Any help?

    Regards,

    Jon

    PS.  Could some more complex examples be added to the FormTemplate demos page for this kind of more advanced scenario?


    Private Sub uxRadTreeList_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.TreeListCommandEventArgs) Handles uxRadTreeList.ItemCommand
        If e.CommandName = Telerik.Web.UI.RadTreeList.EditCommandName Then
            If Not (DirectCast(e.Item, Telerik.Web.UI.TreeListDataItem).ParentItem Is Nothing) Then
                Dim parentSecurityRoleID As String = DirectCast(e.Item, Telerik.Web.UI.TreeListDataItem).ParentItem.Item("SecurityRoleID").Text.ToString()
                Dim parentSecurityRole As String = DirectCast(e.Item, Telerik.Web.UI.TreeListDataItem).ParentItem.Item("SecurityRole").Text.ToString()
     ?????
            End If
        End If
    End Sub
  2. Andrey
    Admin
    Andrey avatar
    836 posts

    Posted 09 Dec 2011 Link to this post

    Hi Jon,

    You could get the RadComboBox which is located in the FormTemplate by referencing the EditFormItem of the RadTreeListDataItem, then to use the FindControl() method over the EditFormItem to find the ComboBox control.

    In means of VB.NET this should be something similar to this:

    Private Sub uxRadTreeList_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.TreeListCommandEventArgs) Handles uxRadTreeList.ItemCommand
        If e.CommandName = Telerik.Web.UI.RadTreeList.EditCommandName Then
            Dim dataItem As TreeListDataItem = TryCast(e.Item, TreeListDataItem)
            Dim editFormItem As TreeListEditFormItem = dataItem.EditFormItem
            Dim comboBox As RadComboBox = TryCast(editFormItem.FindControl("RadComboBox1"), Telerik.Web.UI.RadComboBox)
            If Not (DirectCast(e.Item, Telerik.Web.UI.TreeListDataItem).ParentItem Is Nothing) Then
                Dim parentSecurityRoleID As String = DirectCast(e.Item, Telerik.Web.UI.TreeListDataItem).ParentItem.Item("SecurityRoleID").Text.ToString()
                Dim parentSecurityRole As String = DirectCast(e.Item, Telerik.Web.UI.TreeListDataItem).ParentItem.Item("SecurityRole").Text.ToString()
                Dim selectedIndex as Integer  = comboBox.SelectedIndex
            End If
        End If
    End Sub


    Regards,
    Andrey
    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jon
    Jon avatar
    813 posts
    Member since:
    Oct 2008

    Posted 09 Dec 2011 Link to this post

    Hi Andrey,

    Hmmm I tried that but the dataItem.EditFormItem appears as Nothing.  The dataItem is referenced just fine but with no EditFormItem.  Also the dataItem is nothing when I use the add button in the header of the treelist.  

    If it makes a difference, below is my definition of the command column - it isn't done using the standard method - the reason being that I wanted the new button in the header but not in the datarows.    I'd be surprised if this is the reason for the problem as the commandname is correct.

    <telerik:TreeListTemplateColumn UniqueName="TemplateEditColumn" >
        <HeaderTemplate>
            <asp:ImageButton ID="AddLink" runat="server" CommandName="InitInsert" ImageUrl="/Images/Icons/16/add_blue.png"></asp:ImageButton>
        </HeaderTemplate>
        <ItemTemplate>
            <asp:ImageButton ID="EditLink" runat="server" CommandName="Edit" ImageUrl="/Images/Icons/16/edit.png"></asp:ImageButton>
        </ItemTemplate>
        <HeaderStyle Width="30px" />
        <ItemStyle Width="30px" />
    </telerik:TreeListTemplateColumn>


  5. Answer
    Andrey
    Admin
    Andrey avatar
    836 posts

    Posted 12 Dec 2011 Link to this post

    Hello Jon,

    No the problem is not with your Template column definition. We should modify the last code I have sent you a bit for your particular scenario.

    We need to split the logic into two parts:

    • First, in the ItemCommand event, if the CommandName is EditCommand to set one Boolean field(for example IsEdited) to true.
    • Second, in the ItemDataBound event we need to move all the logic from the ItemCommand event in this event and check if the currently bounded item is TreeListEditFormItem and if the EditFormItem is in edit mode. If all these check have passed we could proceed with the rest of the logic. Namely:

     

    Dim IsEdited As Boolean = False
       Private Sub RadTreeList1_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.TreeListCommandEventArgs) Handles RadTreeList1.ItemCommand
           If e.CommandName = Telerik.Web.UI.RadTreeList.EditCommandName Then
               IsEdited = True
           End If
       End Sub

    And,

    Protected Sub RadTreeList1_ItemDataBound(sender As Object, e As Telerik.Web.UI.TreeListItemDataBoundEventArgs) Handles RadTreeList1.ItemDataBound
            Dim editFormItem As TreeListEditFormItem = TryCast(e.Item, TreeListEditFormItem)
            If editFormItem IsNot Nothing AndAlso editFormItem.IsInEditMode AndAlso IsEdited Then
                Dim parentSecurityRoleID As String = TryCast(editFormItem.ParentItem, TreeListDataItem)("SecurityRoleID").Text.ToString()
                Dim parentSecurityRole As String = TryCast(editFormItem.ParentItem, TreeListDataItem)("SecurityRole").Text.ToString()
                Dim comboBox As RadComboBox = TryCast(editFormItem.FindControl("RadComboBox1"), Telerik.Web.UI.RadComboBox)
                Dim selectedIndex As Integer = comboBox.SelectedIndex
                IsEdited = False
            End If
        End Sub

    I have created sample project to illustrate the aforementioned approach. Give it a try and check whether you have any issues.

    Greetings,
    Andrey
    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
  6. Jon
    Jon avatar
    813 posts
    Member since:
    Oct 2008

    Posted 13 Dec 2011 Link to this post

    Many thanks Andrey, 

    Now it's a matter of accessing the same controls when saving.  I'd normally manually handle the Updating/Inserting event of the datasource.  What would be the best approach for handling this - would it be to get the data for manually bound fields from the form while in the button event handler and then store as variables or is there a way to access the form directly from the Updating/Inserting event?

    Regards,

    Jon
  7. Answer
    Andrey
    Admin
    Andrey avatar
    836 posts

    Posted 13 Dec 2011 Link to this post

    Hello Jon,

     I think getting the EditForm within the Insert/Update command events is the easier approach in your case. You could get reference to the EditForm in the Update/Insert command events by the event argument. Like this:

    For the InsertCommand event.

    Protected Sub RadTreeList1_InsertCommand(sender As Object, e As Telerik.Web.UI.TreeListCommandEventArgs) Handles RadTreeList1.InsertCommand
            Dim insertItem As TreeListEditFormInsertItem = TryCast(e.Item, TreeListEditFormInsertItem)
            Dim comboBox As RadComboBox = TryCast(insertItem.FindControl("RadComboBox1"), RadComboBox)
        End Sub

    And for the UpdateCommand event:

    Protected Sub RadTreeList1_UpdateCommand(sender As Object, e As Telerik.Web.UI.TreeListCommandEventArgs) Handles RadTreeList1.UpdateCommand
            Dim insertItem As TreeListEditFormItem = TryCast(e.Item, TreeListEditFormItem)
            Dim comboBox As RadComboBox = TryCast(insertItem.FindControl("RadComboBox1"), RadComboBox)
        End Sub

    When you get the value for every control in the EditForm the Insert/Update operation to the DataSource should be an easy task for you.

    Greetings,
    Andrey
    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
  8. Jon
    Jon avatar
    813 posts
    Member since:
    Oct 2008

    Posted 14 Dec 2011 Link to this post

    Hi Andrey,

    That's what I suspected would be the case - many thanks.  

    I'll merge this into my code today - it will allow me to get rid of the hidden field I was using to track these.  I think I'll end up also merging the update and insert commands as most of the functionality under them will remain the same.

    I've added a treeview inside a combo that has checkboxes so this method will probably help me greatly with that - it works fine but there is some quite complex JS for managing it.

    Thanks again for your help,

    Jon
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017