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

[Solved] Accessing Edit Template Controls After Set Telerik.Web.UI.GridItem.Edit = true

2 Answers 125 Views
Grid
This is a migrated thread and some comments may be shown as answers.
cheburek
Top achievements
Rank 1
cheburek asked on 15 Dec 2009, 04:35 PM
Hello.
I have rad grid with templated columns (readonly and edit modes).
I set programmatically some row to edit mode base on my data on ItemDataBound event handler and then try to access control from row by its Id. I can't find control from EditTemplate but exist only from ItemTemplate

I explain on simple example. Here is a grid with templated column
<telerik:RadGrid runat="server" ID="weldsGrid" AllowPaging="True" AllowSorting="True" 
            PageSize="20" GridLines="None"
            <MasterTableView AllowMultiColumnSorting="True" AutoGenerateColumns="False" EditMode="InPlace"
                <Columns> 
<telerik:GridTemplateColumn DataField="Welder" HeaderText="Welder"  
                        SortExpression="Welder" UniqueName="Welder"
                        <EditItemTemplate> 
                            <asp:TextBox ID="WelderEdit" runat="server" Text='<%# Bind("Welder")%>' 
                                ontextchanged="WelderTextBox_TextChanged" AutoPostBack="True" /> 
                        </EditItemTemplate> 
                        <ItemTemplate> 
                            <asp:Literal ID="WelderRd" runat="server" Text='<%# Eval("Welder") %>' /> 
                        </ItemTemplate> 
                    </telerik:GridTemplateColumn> 
</Columns> 
            </MasterTableView> 
            <ClientSettings> 
                <Selecting AllowRowSelect="True" /> 
            </ClientSettings> 
        </telerik:RadGrid> 
ItemTemplate contains Literal control with Id = WelderRd   and EditItemTemplate contains control WelderEdit

Here I set one of rows to edit mode when I get data from database.
Private Sub weldsGrid_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles weldsGrid.ItemDataBound 
    If (e.Item.ItemType = Telerik.Web.UI.GridItemType.Item OrElse _ 
        e.Item.ItemType = Telerik.Web.UI.GridItemType.AlternatingItem) Then 
 
      If (e.Item.ItemIndex > -1) Then 
        Dim row = CType(CType(e.Item.DataItem, DataRowView).Row, DSBiasStep.BiasWeldRow) 
        SetControlsVisibility(e.Item, row) 
      End If 
 
    End If 
  End Sub 
 
Private Sub SetControlsVisibility(ByVal row As GridItem, ByVal data As DSBiasStep.BiasWeldRow) 
    'set row to edit mode 
    row.Edit = data.IsActive 
 
    If Not data.IsIsAcceptedNull Then 
      'even row in edit mode next row returns null 
      Dim WelderControl= TryCast(row.FindControl("WelderEdit"), TextBox) 
      If WelderControl Is Nothing Then 
        ''''There is no reason to this but this is just example.  
        'We always find a literal control even row is in edit mode         
        WelderControl = TryCast(row.FindControl("WelderRd"), Literal) 
      End If 
      WelderControl.Enabled = CurrentUser.HasEditorRole 
    End If 
  End Sub 

How can I get controls from EditItemTemplate after setting row to edit mode programmatically?

2 Answers, 1 is accepted

Sort by
0
Johny
Top achievements
Rank 1
answered on 16 Dec 2009, 08:07 AM
Hi Cheburek,

I have tried this code and it ran normally:

Protected Sub weldsGrid_ItemDataBound(ByVal sender As ObjectByVal e As GridItemEventArgs) 
     If TypeOf e.Item Is GridDataItem AndAlso e.Item.IsInEditMode Then 
         Response.Write(TryCast(TryCast(e.Item, GridDataItem).FindControl("WelderEdit"), TextBox).Text) 
     End If 
End Sub 
 
 

I hope this helps.
Johny
0
Princy
Top achievements
Rank 2
answered on 16 Dec 2009, 08:20 AM
Hi,

You can try out the following code to access the Controls in the EditITemTemplate:
vb:
Protected Sub RadGrid1_ItemDatabound(sender As Object, e As GridItemEventArgs) 
    If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then 
        Dim editedItem As GridEditableItem = DirectCast(e.Item, GridEditableItem) 
             
        Dim txtbx As TextBox = DirectCast(editedItem.FindControl("WelderEdit"), TextBox) 
                '... 
    End If 
End Sub 

You can also refer to the following online demo:
Accessing cells and rows

Thanks
Princy.
Tags
Grid
Asked by
cheburek
Top achievements
Rank 1
Answers by
Johny
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or