Can I check please, if my code below is the correct way to retrieve values from grid controls to use a parameter values in a SQL UPDATE query - as the values do not seem to be getting picked up:
Private Sub RDHauls_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RDHauls.UpdateCommand
Try
' List of parameter values that need to be passed into the UPDATE SQL query
TripCode = RTrim(
CStr(ddlTripCode.SelectedItem.Value))
Dim VesselCode As String = CType(e.Item.FindControl("VessReg"), TextBox).Text
Dim HaulNumber As Integer = CType(e.Item.FindControl("HaulNumber"), TextBox).Text
Dim RigNumber As Integer = CType(e.Item.FindControl("RigNumber"), TextBox).Text
Thank you, Ida
7 Answers, 1 is accepted
Please try the following code snippet to access the controls in UpdateCommand.If this doesn't help,please provide your full code snippet.
VB:
Dim
edit
As
GridEditableItem =
DirectCast
(e.Item, GridEditableItem)
'Access a BoundColumn
Dim
id
As
String
= TryCast(edit(
"ID"
).Controls(0), TextBox).Text
'Access a TemplateColumn dropdownlist
Dim
DropDownList1
As
DropDownList = TryCast(edit.FindControl(
"DropDownList1"
), DropDownList)
Dim
strTemp
As
String
= DropDownList1.SelectedItem.Text
Thanks,
Princy
That worked great!
The RadGrid textboxes are getting picked up ok. Though just noticed that the dropdown selected values do not seem to be identified.
Here is the code below that I am using to pick out a dropdown selected value:
Dim
dropdownTechnique As RadComboBox = TryCast(edit.FindControl("Technique"), RadComboBox)
Dim selectedTechnique As Integer = CInt(dropdownTechnique.SelectedValue)
I get an error:
Object reference not set to an instance of an object
So I am guessing that the selected value is not getting assigned to the selectedTechnique variable.
Any advice please?
Thanks, Ida
Below is the sample code snippet that i tried,it works fine at my end.Bind the RadComboBox in the ItemDatabound Event during the edit mode.
ASPX:
<
radG:GridTemplateColumn
UniqueName
=
"ShipCity"
DataField
=
"ShipCity"
HeaderText
=
"ShipCity"
>
<
ItemTemplate
>
<%#DataBinder.Eval(Container.DataItem, "ShipCity")%>
</
ItemTemplate
>
<
EditItemTemplate
>
<
radG:RadComboBox
runat
=
"server"
ID
=
"radComboCity"
/>
</
EditItemTemplate
>
</
radG:GridTemplateColumn
>
VB:
Protected
Sub
grdParkingLocations_UpdateCommand(sender
As
Object
, e
As
GridCommandEventArgs)
Try
Dim
edit
As
GridEditableItem =
DirectCast
(e.Item, GridEditableItem)
Dim
OrderID
As
String
= edit.GetDataKeyValue(
"ID"
).ToString()
Dim
combo
As
RadComboBox =
DirectCast
(edit.FindControl(
"radComboCity"
), RadComboBox)
'code to update
Dim
val
As
String
= combo.SelectedValue
'Error message
Catch
ex
As
Exception
End
Try
End
Sub
Thanks,
Princy
ASPX:
<telerik:GridDropDownColumn DataField="Technique" HeaderText="Technique" ListTextField="Name" ListValueField="Code" UniqueName="Technique" ColumnEditorID="Technique" Visible="false" DropDownControlType="DropDownList"></telerik:GridDropDownColumn>
VB.NET:
Private Sub RDHauls_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RDHauls.UpdateCommand
Dim edit As GridEditableItem = DirectCast(e.Item, GridEditableItem)
Dim dropdownTechnique As RadComboBox = TryCast(edit.FindControl("Technique"), RadComboBox)
Dim selectedTechnique As Integer = dropdownTechnique.SelectedValue
The selected value is not being found. Should I change my dropdown set-up?
Thanks, Ida
I set up my dropdown to match your sample code:
<telerik:GridTemplateColumn UniqueName="Technique" DataField="Technique" HeaderText="Technique">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "Technique")%>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox runat="server" ID="Technique" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
But the databinding VB.NET code would not work:
Protected Sub RDHauls_ItemDataBound(sender As Object, e As GridItemEventArgs)
If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
Dim editItemTechnique As GridEditableItem = DirectCast(e.Item, GridEditableItem)
Dim editManagerTechnique As GridEditManager = editItemTechnique.EditManager
Dim editorTechnique As GridDropDownListColumnEditor = DirectCast(editManagerTechnique.GetColumnEditor("Technique"), GridDropDownListColumnEditor)
Dim dsTechnique As New DSHaulsTableAdapters.DisGetTechniquesForGearsTableAdapter
editorTechnique.DataSource = dsTechnique.GetDisGetTechniquesForGears(TripCode)
Dim selectedTechnique As Integer = DataBinder.Eval(editItemTechnique.DataItem, "Technique")
Dim rComboBoxTechnique As DropDownList = editorTechnique.DropDownListControl
rComboBoxTechnique.DataTextField = "Name"
rComboBoxTechnique.DataValueField = "Code"
rComboBoxTechnique.DataBind()
rComboBoxTechnique.SelectedValue = selectedTechnique
I get the following error message:
Unable to cast object of type 'Telerik.Web.UI.GridTemplateColumnEditor' to type 'Telerik.Web.UI.GridDropDownListColumnEditor'
Dim
dropdownTechnique As DropDownList = TryCast(edit("Technique").Controls(0), DropDownList)
instead of
Dim dropdownTechnique As RadComboBox = TryCast(edit.FindControl("Technique"), RadComboBox)
Thanks for your help!
Ida