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

Update controls when an item in RadGrid is clicked

4 Answers 72 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Arno
Top achievements
Rank 2
Arno asked on 22 Oct 2010, 07:43 PM
Hello,

I'm working on a scenario similar to this demo, where a click on an item in a RadGrid updates other RadGrids through AJAX. That part works fine. Now, I would like the header above the automatically updated RadGrids to be updated too, based on the clicked item. The code below allows me to get the ID which was clicked in the main RadGrid.

    Private Sub RadGridResellers_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGridResellers.ItemCommand
        If (e.CommandName = "RowClick") Then
            Dim GridItem As Telerik.Web.UI.GridDataItem = e.CommandSource
            Dim ResellerID As Short = GridItem.GetDataKeyValue("id")
wState)
            ... more code ...
        End If
    End Sub

However, this event seems to be too late in the page life cycle to make changes to the header or any other control. I can store the ID into the viewstate and make my changes on a second postback, but that's not very elegant. Is there another way?

4 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 27 Oct 2010, 09:37 AM
Hello AP,

Try the following appraoch: In the ItemCommand event handler of the first RadGrid, save the data key value into a temporary variable. Use the ItemCreated event of the related RadGrid to check for the header item and make the necessary modificiations:

Protected Sub RelatedGrid_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RelatedGrid.ItemCreated
    If TypeOf e.Item Is GridHeaderItem Then
        Dim header As GridHeaderItem = DirectCast(e.Item, GridHeaderItem);
        'modify the header item here
    Else If
End Sub


Veli
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Arno
Top achievements
Rank 2
answered on 29 Oct 2010, 10:20 AM
Hi Veli,

Thanks for looking into this. The problem with the approach you suggest is that ItemCreated is fired before ItemCommand. So, in ItemCreated, the data is not there yet.

In other words: ItemCommand is too late in the page life cycle to make any modifications to the page. Isn't there an earlier event to get the ID of the clicked item?
0
Veli
Telerik team
answered on 29 Oct 2010, 10:54 AM
Hi AP,

ItemCreated will be re-fired every time you rebind the grid. So, consider the following scenario:

1. An item is selected in the first RadGrid
2. The first RadGrid posts the page
3. ItemCreated is fired for the second RadGrid.
4. ItemCommand fires for the first RadGrid. You rebind the second grid in the ItemCommand event handler of the first grid
5. ItemCreated is fired again for the second RadGrid.

In the above scenario, point 3 and 5 indicate ItemCreated will be fired once when the grid loads from ViewState and once when it is rebound. This is why you can use ItemCreated for scenarios when you rebind the grid.

Otherwise, if you want to use a page event that is late enough so that all data is set in both grids, you can try PreRender:

protected void Page_PreRender(object sender, EventArgs e)
{
    GridHeaderItem header = (GridHeaderItem)RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0];
    //modify the cells in the header item here
}

You can also use ItemDataBound, but this event is not fired if RadGrid is not rebound in the current postback, so you cannot use it for action that needs to happen on every postback.

Veli
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Arno
Top achievements
Rank 2
answered on 29 Oct 2010, 11:23 AM
Thanks Veli, that's interesting. In the meantime, I found that there are other ways to retrieve the clicked item than just from ItemCommand. I'm now getting it from the SelectedValue property of the RadGrid:

CType(RadGridResellers.SelectedValue, Short)

That seems to work fine. Good to know though, that ItemCreated is actually fired on both loading and binding.

Thanks!
Tags
Grid
Asked by
Arno
Top achievements
Rank 2
Answers by
Veli
Telerik team
Arno
Top achievements
Rank 2
Share this question
or