Scroll to newly added row in RadGrid

40 Views Asked by At

After I add a row to my RadGrid, I want to select that row, which is always at the bottom of the RadGrid. This isn't working for mysterious reasons.

I'm adding the record using my own procedure, so I can validate it before updating:

' Select the newly-added route
AvailableRoutesList.DataBind()
For Each routeInList As GridDataItem In AvailableRoutesList.MasterTableView.Items
    If routeInList.Item("Route").Text = route Then
        routeInList.Selected = True
        Dim index As Integer = routeInList.ItemIndex
        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "ScrollToRoute",
                                String.Format("scrollToRoute('{0}', '{1}');",
                                                index, AvailableRoutesList.ClientID),
                                True)
        Exit For
    End If
Next

The intention is to use the Javascript scrollToRoute function to scroll to that selected route:

function scrollToRoute(gridId, radGridClientID) {
    var grid = document.getElementById(radGridClientID);
    var selectedItem = grid.get_selectedItems()[0];
    if (selectedItem) {
        var scrollableElement = grid.get_element();
        var selectedItemElement = selectedItem.get_element();
        var scrollTop = selectedItemElement.offsetTop - scrollableElement.offsetTop;
        scrollableElement.scrollTop = scrollTop;
    }
}

This is currently failing on get_element() which it says isn't a property of my grid. It seems grid is being selected as a div element instead of the RadGrid.

As I researched online, I tried to use $find instead, but $find(radGridClientID) returns null, even though document.getElementById(radGridClientID) returns the div element.

I don't understand how I'm supposed to select the RadGrid from Javascript so that the scrolling function will work. Any ideas?

EDIT: New insight which might help: my RadScriptManager is on my Site.master page and is not in a RadAjaxPanel object. I'm hesitant to put it in one, since this would affect a bunch of other pages, and I don't know the ramifications. Does this help?

0

There are 0 best solutions below