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

How to Find Control in EditFormSettings \ FormTemplate in a Grid

2 Answers 337 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lucas
Top achievements
Rank 1
Lucas asked on 27 Aug 2013, 02:42 PM
Hi, i have a check box to edit normal or admin user, I'd like to turn this checkbox visible to False if the User to be edited is the same logged. this is the code I have.
Resumed code
<MasterTableView AutoGenerateColumns="false" EditMode="EditForms" CommandItemDisplay="Top" DataKeyNames="name, admin ">
 <Columns>
  <telerik:GridBoundColumn UniqueName="name" DataField="name" meta:resourceKey="name"/>                        
  <telerik:GridCheckBoxColumn UniqueName="admin" DataField="admin" DefaultInsertValue="False" DataType="System.Boolean" meta:resourceKey="Admin"/>                                              
  <telerik:GridEditCommandColumn HeaderStyle-Width="100px" meta:resourceKey="EditLnk"/>                        
  <telerik:GridButtonColumn CommandName="Acervos" UniqueName="AcervosColumn" HeaderStyle-Width="100px" meta:resourceKey="AcervosLnk"  />
  <telerik:GridButtonColumn CommandName="Delete" UniqueName="DeleteColumn" HeaderStyle-Width="100px" meta:resourceKey="DeleteLnk"/>                                                
  </Columns>
   <SortExpressions>
   <telerik:GridSortExpression FieldName="name" SortOrder="Ascending" />
   </SortExpressions> 
   <EditFormSettings EditFormType="Template" >
   <FormTemplate >
    <div style="width: 300px" >
    <div style="margin:5px;">
     <asp:Label ID="NameLbl" runat="server" meta:resourceKey="NameLbl"/>&nbsp<asp:TextBox ID="NameTxt" runat="server" Text='<%# Bind("name") %>'/>
    </div> 
     <div style="margin:5px;">
       <asp:Label ID="PassLbl" runat="server" meta:resourceKey="PassLbl"/>&nbsp<asp:TextBox ID="PassTxt" runat="server" Text='<%# Bind("pass") %>' TextMode="Password" /> 
     </div> 
     <div style="margin:5px;">
       <asp:CheckBox ID="AdminChk" runat="server" meta:resourceKey="AdminChk" Checked='<%# Bind("admin") %>' />                                  </div> 
     <div style="margin:10px;text-align:right;">
     <telerik:RadButton ID="btnUpdate" Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), AddJS, UpdateJS) %>' runat="server" CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>'/>
     <telerik:RadButton ID="btnCancel" runat="server" CausesValidation="False" CommandName="Cancel" meta:resourceKey="CancelBtn" />
     </div> 
     </div>
     </FormTemplate>
     </EditFormSettings>
     </MasterTableView>

Code Behind 

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

        If e.CommandName = RadGrid.EditCommandName Then

            Dim Usuario As String = HttpContext.Current.User.Identity.Name.ToLower

            If Usuario = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("name").ToString().ToLower Then
                Dim editItem As GridEditFormItem = CType(e.Item, GridEditFormItem)
                Dim ChkBxItem As CheckBox = CType(editItem.FindControl("AdminChk"), CheckBox)
                ChkBxItem.Checked = False
            End If
        End If

 Every time I run, it shows a message! Can not convert an object of type 'Telerik.Web.UI.GridDataItem' in type 'Telerik.Web.UI.GridEditFormItem'. 

How can I solve this?

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 28 Aug 2013, 04:00 AM
Hi Lucas,

If you need to make certain controls invisible or disabled on edit/insert, a more suitable event would be the ItemDataBound event of RadGrid.Please try the below code snippet.

VB:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs)
    If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode Then
        Dim edit As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
        Dim chk As CheckBox = DirectCast(edit.FindControl("AdminChk"), CheckBox)
        ' Your Condition
        If True Then
            'Hide checkbox 
            chk.Visible = False
        End If
    End If
End Sub

Thanks,
Princy

0
Lucas
Top achievements
Rank 1
answered on 28 Aug 2013, 01:02 PM
Good morning,

  worked perfectly



thank you Princy

Tags
Grid
Asked by
Lucas
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Lucas
Top achievements
Rank 1
Share this question
or