How to Find Control in EditFormSettings \ FormTemplate

5 posts, 1 answers
  1. Ron
    Ron  avatar
    2 posts
    Member since:
    Feb 2009

    Posted 07 May 2009 Link to this post

    I have a combo box and a button inside of a EditFormSettings \ FormTemplate.  In the Items_command event of the grid I trap the buitton click event and I'm tryiong to enable or disable it.

    Here is the page code



    telerik:RadGrid ID="RadGrid1" runat="server">



    <HeaderContextMenu EnableTheming="True">



    <CollapseAnimation Type="OutQuint" Duration="200">









    <MasterTableView CommandItemDisplay="Top" DataKeyNames="Contact_ID" Width="100%"



    ExpandCollapseColumn-Display="true" HierarchyDefaultExpanded="false"



    EditMode="EditForms" AllowPaging="True" PageSize="5">






    <span lang="en-us">ITEM TEMPLATE</span>









    <HeaderStyle Width="20px"></HeaderStyle>









    <HeaderStyle Width="20px"></HeaderStyle>






    <EditFormSettings EditFormType="Template">






    <asp:DropDownList ID="cboCountry" runat="server" Enabled="false">






    <asp:Button runat="server" ID="butCountry" text="change" CausesValidation="False"



    CommandName="ChangeCountry" />



    <br />



    <br />












    <FilterMenu EnableTheming="True">



    <CollapseAnimation Type="OutQuint" Duration="200">











    and here is the Item_command event in the code behind


    Private Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand



    strCommand As String


    strCommand = e.CommandName.ToUpper



    Case strCommand



        Case Is = "CHANGECOUNTRY"



           Dim editItem As GridDataItem = CType(e.Item, GridDataItem)




    ContactCombo As DropDownList = CType(editItem.FindControl("cboCountry"), DropDownList)


            ContactCombo.Enable = "True"


        End Select



    End Sub


    Why is it that I can't get a handle to the combo box ?

    Any help would be great !
  2. Answer
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 07 May 2009 Link to this post

    Hello Ron,

    You would have to cast the controls in the EditForm as EditFormItem/EditableItem and not as GridDataItem:
    Private Sub RadGrid1_ItemCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand   
     Dim strCommand As String strCommand = e.CommandName.ToUpper   
      Select Case strCommand  
        Case Is = "CHANGECOUNTRY"   
             Dim editItem As GridEditFormItem = CType(e.Item, GridEditFormItem)   
             Dim ContactCombo As DropDownList = CType(editItem.FindControl("cboCountry"), DropDownList)  
             ContactCombo.Enable = "True"  
         End Select   
    End Sub  

  3. DevCraft R3 2016 release webinar banner
  4. Ron
    Ron  avatar
    2 posts
    Member since:
    Feb 2009

    Posted 11 May 2009 Link to this post


    Worked perfectly. You are truly of royalty !

  5. ram sagar reddy
    ram sagar reddy avatar
    1 posts
    Member since:
    Aug 2015

    Posted 18 Aug 2015 in reply to Princy Link to this post

    An exception of type 'System.InvalidCastException' occurred in DCSE.iAPECS.Web.UI.dll but was not handled in user code

    Additional information: Unable to cast object of type 'Telerik.Web.UI.GridCommandItem' to type 'Telerik.Web.UI.GridEditFormItem'.
  6. Viktor Tachev
    Viktor Tachev avatar
    1445 posts

    Posted 20 Aug 2015 Link to this post

    Hi Ram,

    The error you are seeing is caused because you are trying to case a GridCommandItem as GridEditFormItem.

    In order to resolve the issue you can add a check for the type of the item:

    Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
        If TypeOf e.Item Is GridEditFormItem Then
            Dim editFormItem As GridEditFormItem = TryCast(e.Item, GridEditFormItem)
        End If
    End Sub

    Viktor Tachev
    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 Feedback Portal and vote to affect the priority of the items
Back to Top
DevCraft R3 2016 release webinar banner