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
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

try with below code snippet.
Dim
editedItem
As
GridEditFormItem =
Nothing
Thanks,
Jayesh Goyani


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


Can you please provide your code?
Thanks,
Jayesh Goyani


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">
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
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

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

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

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

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

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


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

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
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