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

EditForm Template editedItem FindControl doesn't work

16 Answers 157 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jay
Top achievements
Rank 1
Veteran
Jay asked on 24 Apr 2012, 04:37 PM
I have a situation similar to this one. Basically, in the edit form I've got a dropdown list to which the user is able to use a popup to select a new value which triggers a postback so the value can be added to the popup and be selected in the popup. In the code-behind, I do
Dim editedItem As GridEditableItem = Nothing
If RadGrid1.MasterTableView.IsItemInserted Then
    editedItem = RadGrid1.MasterTableView.GetInsertItem()
End If
If IsNothing(editedItem) Then
    editedItem = RadGrid1.EditItems.Item(0)
End If
  
Dim ddl As DropDownList = CType(editedItem.FindControl("ddlId"), DropDownList)
ddl.Items.Insert(0, newValue)
ddl.SelectedValue = newValue
This works when I am adding a new item. However, if I am editing an existing item, the drop down list is not found; ddl is Nothing and I get a NullReferenceException.

In stepping through the code, in the add case, editedItem has a value of GridEditFormInsertItem with a type of GridEditableItem. However, in the edit case, its value is a GridDataItem with a type of GridEditableItem and its controls appear to be for the regular grid display rather than the edit form.

Any thoughts on how I can get this to work in the edit case?

16 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 25 Apr 2012, 07:33 AM
Hello Jay,

try with below code snippet.
Dim editedItem As GridEditFormItem = Nothing



Thanks,
Jayesh Goyani
0
Jay
Top achievements
Rank 1
Veteran
answered on 25 Apr 2012, 01:10 PM
Thanks for the suggestion, but that results in an InvalidCastException: Unable to cast object of type 'Telerik.Web.UI.GridDataItem' to type 'Telerik.Web.UI.GridEditFormItem'.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 25 Apr 2012, 01:47 PM
Hello Jay,

Try with below code snippet.

For Each item As GridEditFormItem In RadGrid1.EditItems
    Dim ddlId As DropDownList = TryCast(item.FindControl("ddlId"), DropDownList)
Next
 
'or
 
For Each item As GridEditableItem In RadGrid1.EditItems
    Dim ddlId As DropDownList = TryCast(item.FindControl("ddlId"), DropDownList)
Next



Thanks,
Jayesh Goyani
0
Jay
Top achievements
Rank 1
Veteran
answered on 25 Apr 2012, 01:55 PM
Yeah, I had already thought of something like that. Problem is there's only one item in RadGrid1.EditItems, and it's a GridDataItem, not a GridEditableItem.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 26 Apr 2012, 04:48 AM
Hello Jay,

Can you please provide your code?

Thanks,
Jayesh Goyani
0
Jay
Top achievements
Rank 1
Veteran
answered on 26 Apr 2012, 07:24 PM
I can't post the whole app, but I'll try to hack up a test example.
0
Jay
Top achievements
Rank 1
Veteran
answered on 30 Apr 2012, 02:37 PM
Here's a simple example. If you add a new record and click the Find drop down list button, it finds it. If you edit an existing record, it won't.

Here is the Default.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="EditBug._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
    <telerik:RadStyleSheetManager id="RadStyleSheetManager1" runat="server" />
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        <Scripts>
            <%--Needed for JavaScript IntelliSense in VS2010--%>
            <%--For VS2008 replace RadScriptManager with ScriptManager--%>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
        </Scripts>
    </telerik:RadScriptManager>
    <script type="text/javascript">
    </script>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    </telerik:RadAjaxManager>
    <div>
 
    </div>
    <telerik:RadGrid ID="RadGrid1" runat="server" BackColor="White" AllowSorting="true" AllowPaging="true" PageSize="100" AllowFilteringByColumn="false" ShowStatusBar="false" Skin="Simple">
        <MasterTableView DataKeyNames="ID" AutoGenerateColumns="false" Width="90%" CommandItemDisplay="Top" >
            <Columns>
                <telerik:GridButtonColumn UniqueName="Edit" Text="Edit" CommandName="Edit" />
                <telerik:GridBoundColumn DataField="Value" UniqueName="Value" HeaderText="Value"/>
            </Columns>
            <EditFormSettings EditFormType="Template">
                <FormTemplate>                                 
                    <table>
                        <tr>
                            <td>File Name</td>
                            <td>
                                <asp:DropDownList ID="ddlFileName" runat="server" />
                                <asp:Button ID="btnFileName" runat="server" OnClick="UpdateFileName" Text="Check for Drop Down List" />
                            </td>
                        </tr>
                    </table>
                    <div>
                        <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="false" CommandName="Cancel" />
                    </div>
                </FormTemplate>
            </EditFormSettings>
            <CommandItemSettings ShowAddNewRecordButton="true" ShowRefreshButton="false" ShowExportToWordButton="false" ShowExportToExcelButton="false" ShowExportToCsvButton="false" />
        </MasterTableView>
    </telerik:RadGrid>
    </form>
</body>
</html>


And here is the code behind
Imports Telerik.Web.UI
 
Partial Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub
 
 
    Private Sub RadGrid1_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        Dim names As List(Of Data) = New List(Of Data)()
        Dim d As New Data
        d.ID = 1
        d.Value = "One"
        names.Add(d)
 
        d = New Data
        d.ID = 2
        d.Value = "Two"
        names.Add(d)
 
        RadGrid1.DataSource = names
        RadGrid1.VirtualItemCount = names.Count
    End Sub
 
    Private Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
        If ((TypeOf e.Item Is GridEditableItem) And (e.Item.IsInEditMode)) Then
            Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
            editedItem.Selected = True
 
            Dim ddlFileName As DropDownList = CType(editedItem.FindControl("ddlFileName"), DropDownList)
            ddlFileName.Items.Insert(0, "")
            ddlFileName.Items.Insert(1, "One")
            ddlFileName.Items.Insert(2, "Two")
            ddlFileName.Items.Insert(3, "Three")
        End If
    End Sub
 
 
    Public Sub UpdateFileName(ByVal sender As Object, ByVal e As EventArgs)
        Dim editedItem As GridEditableItem = Nothing
        If RadGrid1.MasterTableView.IsItemInserted Then
            editedItem = RadGrid1.MasterTableView.GetInsertItem()
        End If
        If IsNothing(editedItem) Then
            editedItem = RadGrid1.EditItems.Item(0)
        End If
 
        Dim ddlFileName As DropDownList = CType(editedItem.FindControl("ddlFileName"), DropDownList)
        If IsNothing(ddlFileName) Then
            Debug.WriteLine("Did NOT find it!")
        Else
            Debug.WriteLine("Found it!")
        End If
    End Sub
 
    Private Class Data
        Private _id As Integer
        Public Property ID() As Integer
            Get
                Return _id
            End Get
            Set(ByVal value As Integer)
                _id = value
            End Set
        End Property
 
        Private _value As String
        Public Property Value() As String
            Get
                Return _value
            End Get
            Set(ByVal value As String)
                _value = value
            End Set
        End Property
 
    End Class
End Class
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Apr 2012, 02:52 PM
Hello Jay,

Please try with below code snippet.

Public Sub UpdateFileName(sender As Object, e As EventArgs)
    Dim btnFileName As Button = TryCast(sender, Button)
 
    Dim editedItem As GridEditableItem = TryCast(btnFileName.NamingContainer, GridEditableItem)
 
    Dim ddlFileName As DropDownList = DirectCast(editedItem.FindControl("ddlFileName"), DropDownList)
 
    If (ddlFileName Is Nothing) Then
 
    Else
    End If
End Sub


Let me know it working or not.

Thanks,
Jayesh Goyani
0
Jay
Top achievements
Rank 1
Veteran
answered on 01 May 2012, 02:07 PM
Yes, that works, thanks!

For completeness, I also received another solution from Telerik support which is perhaps a bit cleaner:
If IsNothing(editedItem) Then
    editedItem = RadGrid1.EditItems.Item(0)
    ddlFileName = CType((CType(editedItem, GridDataItem)).EditFormItem.FindControl("ddlFileName"), DropDownList)
End If

with the following explanation:

Please note that the grid's EditItems collection contains the items (rows) of the grid that are currently being edited. When using InPlace edit mode, these items contain the edit controls. When using EditForms (as in your case) or PopUp however, the items in the collection are the items in their initial (unedited) form. To access the edit form for an item in the list, use its EditFormItem property.

The following help topic provides more information on the matter: Edit Forms
0
Kiran
Top achievements
Rank 2
answered on 04 Feb 2019, 03:31 PM

Hi,

I am still not able to resolve my problem of getting new values in the textbox (OPR COUNT) of edit form template. It keeps giving me the actual value instead of user entered or new value. Below is my code. I also attached the ASPX side of the page. 

If e.CommandName = "Update" Then
            Try
                If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then
                    Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
                    Dim OprCount As TextBox = CType(editedItem.FindControl("txtOprCount"), TextBox)
                    Dim strRcvCount As String = OprCount.Text

                    Dim strUserName As String = Session("USERNAME")
                    Dim strSYSID As String = rgRtnDetail.MasterTableView.DataKeyValues(e.Item.ItemIndex)("SYS_ID")

                    strSQlData = "EXECUTE SP_IB_RETURNS_DET_UPDATE '" & strUserName & "' ,'" & strSYSID & "', '" & strRcvCount & "'"

                    Dim errcode As String
                    Dim nRecCount As Integer

                    Dim myCommand As New SqlCommand(strSQlData, oConn)
                    Dim da = New SqlDataAdapter(strSQlData, oConn)
                    Dim ds = New DataSet
                    da.Fill(ds, "ERROR")
                    dt = ds.Tables(0)
                    nRecCount = dt.Rows.Count

                    Response.Redirect(Request.RawUrl)
                End If
            Catch ex As Exception
                RadWindowManager1.RadAlert("Problem Updating the Record!", 250, 150, "ERROR", "")
            End Try
        End If

0
Kiran
Top achievements
Rank 2
answered on 04 Feb 2019, 03:33 PM

I am still not able to retrieve new value from edit form template. Please help.

If e.CommandName = "Update" Then
            Try
                If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then
                    Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
                    Dim OprCount As TextBox = CType(editedItem.FindControl("txtOprCount"), TextBox)
                    Dim strRcvCount As String = OprCount.Text
                    MsgBox(strRcvCount)
                    Exit Sub
                    Dim strUserName As String = Session("USERNAME")
                    Dim strSYSID As String = rgRtnDetail.MasterTableView.DataKeyValues(e.Item.ItemIndex)("SYS_ID")

                    strSQlData = "EXECUTE SP_IB_RETURNS_DET_UPDATE '" & strUserName & "' ,'" & strSYSID & "', '" & strRcvCount & "'"

                    Dim errcode As String
                    Dim nRecCount As Integer

                    Dim myCommand As New SqlCommand(strSQlData, oConn)
                    Dim da = New SqlDataAdapter(strSQlData, oConn)
                    Dim ds = New DataSet
                    da.Fill(ds, "ERROR")
                    dt = ds.Tables(0)
                    nRecCount = dt.Rows.Count

                    Response.Redirect(Request.RawUrl)
                End If
            Catch ex As Exception
                RadWindowManager1.RadAlert("Problem Updating the Record!", 250, 150, "ERROR", "")
            End Try
        End If

 

0
Kiran
Top achievements
Rank 2
answered on 04 Feb 2019, 03:33 PM

I am still not able to resolve this. Textbox of edit form template nottaking the latest value to update.

 

If e.CommandName = "Update" Then
            Try
                If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then
                    Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
                    Dim OprCount As TextBox = CType(editedItem.FindControl("txtOprCount"), TextBox)
                    Dim strRcvCount As String = OprCount.Text
                    MsgBox(strRcvCount)
                    Exit Sub
                    Dim strUserName As String = Session("USERNAME")
                    Dim strSYSID As String = rgRtnDetail.MasterTableView.DataKeyValues(e.Item.ItemIndex)("SYS_ID")

                    strSQlData = "EXECUTE SP_IB_RETURNS_DET_UPDATE '" & strUserName & "' ,'" & strSYSID & "', '" & strRcvCount & "'"

                    Dim errcode As String
                    Dim nRecCount As Integer

                    Dim myCommand As New SqlCommand(strSQlData, oConn)
                    Dim da = New SqlDataAdapter(strSQlData, oConn)
                    Dim ds = New DataSet
                    da.Fill(ds, "ERROR")
                    dt = ds.Tables(0)
                    nRecCount = dt.Rows.Count

                    Response.Redirect(Request.RawUrl)
                End If
            Catch ex As Exception
                RadWindowManager1.RadAlert("Problem Updating the Record!", 250, 150, "ERROR", "")
            End Try
        End If

0
Kiran
Top achievements
Rank 2
answered on 04 Feb 2019, 03:34 PM
hi
0
Kiran
Top achievements
Rank 2
answered on 04 Feb 2019, 03:34 PM
If e.CommandName = "Update" Then
            Try
                If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then
                    Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
                    Dim OprCount As TextBox = CType(editedItem.FindControl("txtOprCount"), TextBox)
                    Dim strRcvCount As String = OprCount.Text
                    MsgBox(strRcvCount)
                    Exit Sub
                    Dim strUserName As String = Session("USERNAME")
                    Dim strSYSID As String = rgRtnDetail.MasterTableView.DataKeyValues(e.Item.ItemIndex)("SYS_ID")

                    strSQlData = "EXECUTE SP_IB_RETURNS_DET_UPDATE '" & strUserName & "' ,'" & strSYSID & "', '" & strRcvCount & "'"

                    Dim errcode As String
                    Dim nRecCount As Integer

                    Dim myCommand As New SqlCommand(strSQlData, oConn)
                    Dim da = New SqlDataAdapter(strSQlData, oConn)
                    Dim ds = New DataSet
                    da.Fill(ds, "ERROR")
                    dt = ds.Tables(0)
                    nRecCount = dt.Rows.Count

                    Response.Redirect(Request.RawUrl)
                End If
            Catch ex As Exception
                RadWindowManager1.RadAlert("Problem Updating the Record!", 250, 150, "ERROR", "")
            End Try
        End If
0
Kiran
Top achievements
Rank 2
answered on 04 Feb 2019, 03:34 PM

If e.CommandName = "Update" Then
            Try
                If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then
                    Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
                    Dim OprCount As TextBox = CType(editedItem.FindControl("txtOprCount"), TextBox)
                    Dim strRcvCount As String = OprCount.Text
                    MsgBox(strRcvCount)
                    Exit Sub
                    Dim strUserName As String = Session("USERNAME")
                    Dim strSYSID As String = rgRtnDetail.MasterTableView.DataKeyValues(e.Item.ItemIndex)("SYS_ID")

                    strSQlData = "EXECUTE SP_IB_RETURNS_DET_UPDATE '" & strUserName & "' ,'" & strSYSID & "', '" & strRcvCount & "'"

                    Dim errcode As String
                    Dim nRecCount As Integer

                    Dim myCommand As New SqlCommand(strSQlData, oConn)
                    Dim da = New SqlDataAdapter(strSQlData, oConn)
                    Dim ds = New DataSet
                    da.Fill(ds, "ERROR")
                    dt = ds.Tables(0)
                    nRecCount = dt.Rows.Count

                    Response.Redirect(Request.RawUrl)
                End If
            Catch ex As Exception
                RadWindowManager1.RadAlert("Problem Updating the Record!", 250, 150, "ERROR", "")
            End Try
        End If

 

0
Attila Antal
Telerik team
answered on 13 Feb 2019, 04:41 PM
Hi Kiran,

I advise double checking the project and compare that to the instructions described in the Accessing Controls in Edit/Insert Mode article to get help on accessing controls in different scenarios.

To be able to tell you more, I would need to see the markup you have used for this scenario.

You may also try to compare the project at your side with the following example and see what is done differently:

Given the following markup, I have set the EditFormType to Template, then in the FormTemplate, I have added a RadTextBox and couple of buttons.
<telerik:RadGrid ID="RadGrid1" runat="server" OnItemCommand="RadGrid1_ItemCommand">
    <MasterTableView DataKeyNames="OrderID">
        <EditFormSettings EditFormType="Template">
            <FormTemplate>
                <telerik:RadLabel ID="RadLabel1" runat="server" AssociatedControlID="RadTextBox1" Text="ShipName: "></telerik:RadLabel>
                <telerik:RadTextBox ID="RadTextBox1" runat="server" Text='<%# Bind("ShipName") %>'></telerik:RadTextBox>
 
                <telerik:RadButton ID="Button1" Text='<%# IIf(DataBinder.Eval(Container, "OwnerTableView.IsItemInserted"), "Insert", "Update") %>'
                    runat="server" CommandName='<%# IIf( DataBinder.Eval(Container, "OwnerTableView.IsItemInserted"), "PerformInsert", "Update") %>'>
                </telerik:RadButton>
            </FormTemplate>
        </EditFormSettings>
        <Columns>
            <%-- Some columns --%>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

In the ItemCommand, the code captures the Update command, will check if the Item is in Edit Mode then cast the e.Item object to a GridEditFormItem. Find the desired controls inside it and access their values.

Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
    If e.CommandName = "Update" Then
        Try
            If (e.Item.IsInEditMode) Then
                Dim editFormItem = DirectCast(e.Item, GridEditFormItem)
                Dim rTextBox = DirectCast(editFormItem.FindControl("RadTextBox1"), RadTextBox)
 
                Dim newValue As String = rTextBox.Text;
 
                Response.Redirect(Request.RawUrl)
            End If
        Catch ex As Exception
            RadWindowManager1.RadAlert("Problem Updating the Record!", 250, 150, "ERROR", "")
        End Try
    End If
End Sub

I hope this will prove helpful.

Kind regards,
Attila Antal
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Jay
Top achievements
Rank 1
Veteran
Answers by
Jayesh Goyani
Top achievements
Rank 2
Jay
Top achievements
Rank 1
Veteran
Kiran
Top achievements
Rank 2
Attila Antal
Telerik team
Share this question
or