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

Event for _dataItem bound?

3 Answers 9 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ItinerantEngineer
Top achievements
Rank 1
ItinerantEngineer asked on 04 Feb 2019, 07:57 PM

I have a usercontrol as my edit form, and a property called DataItem that gets bound, one item of which is called ItemID and is bound to a HiddenField. 

<asp:HiddenField ID="itemID" runat="server" Visible="false" Value='<%# DataBinder.Eval(Container, "DataItem.ItemID") %>' />

Everything seems good, form renders. I have two controls I call by button click, and in those handlers I can get the value of the HiddenField ItemID to pass its value along in a url.

"&ItemID=" & itemID.Value.ToString() &

 

The problem comes in when in Page_Load, I want to restrict whether to show these controls or not by the value of ItemID.  In Page_Load, itemID.Value is empty, presumably because the page hasn't loaded yet, and while _dataItem seems to exist, it has no fields yet.  Is there an event I can tie into to say 'run this after _dataItem has been bound'? There is OnDataBound, but the note on the documentation page says only useful with client side data binding and my solution uses NeedsDataSource.

3 Answers, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 11 Feb 2019, 07:32 PM
Hi,

I advise using the ItemDataBound event of the grid. That's one of the earliest event where the data is known and you you can use it to execute further logic based on the values. You can check out the Event sequence article for list of events fired by the grid at specific times.

For example, within the ItemDataBound event handler, you can follow the instructions for Accessing Controls in Edit/Insert Mode from WebUserControls:

Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
        Dim editableItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
 
        Dim userControl As UserControl = CType(editableItem.FindControl(GridEditFormItem.EditFormUserControlID), UserControl)
        Dim hf As HiddenField = CType(userControl.FindControl("itemID"), HiddenField)
        Dim ItemID = CType(editableItem.GetDataKeyValue("ItemID"), Integer)
        '' Display the HiddenField for item with even IDs
        hf.Visible = ItemID Mod 2 = 0
    End If
End Sub

I hope this will be 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.
0
ItinerantEngineer
Top achievements
Rank 1
answered on 12 Feb 2019, 06:58 PM

Thank you for the answer, but you misunderstood my need.  I needed to get the value of an item from the parent inside my edit usercontrol and not the grid page.  In case others need the same thing, this is what I found to be the answer for getting values in the Page_Load function

Dim editedItem As GridEditableItem = Me.Parent.NamingContainer
Dim parentItem = DirectCast(editedItem.OwnerTableView.ParentItem, GridDataItem)
Dim ItemUID = parentItem.GetDataKeyValue("ItemUID").ToString()
0
Attila Antal
Telerik team
answered on 15 Feb 2019, 12:17 PM
Hi,

Thank you for sharing you'r findings with the community. I hope it will help other developers in the future if working with a similar scenario.

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
ItinerantEngineer
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
ItinerantEngineer
Top achievements
Rank 1
Share this question
or