1

Here is what my button click event looks like:

protected void btnSave_Click(object sender, EventArgs e)
{
    // Save logic goes here
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "saveCallback", "SaveGridChanges();", true);
}

As you can see I am trying to call the function SaveGridChanges(); js function after the save logic runs.

function SaveGridChanges()
{
    var radGrid1 = $find('<%=RadGrid1.ClientID%>');
    radGrid1.get_batchEditingManager().saveChanges(radGrid1.get_masterTableView());
}

The problem is that I am getting JavaScript errors when the function is called because it is unable to find the grid. This is most likely because the script is trying to run before the grid is output on the page. Any suggestions on what I can do here?

The exact error messages are:

Uncaught TypeError: Cannot read property 'get_batchEditingManager' of null. Uncaught TypeError: Cannot read property 'disable' of null.

I have tried registering the script on both page_load and pre_render and the grid still isn't available at those times. This makes no sense.

Blake Rivell
  • 13,105
  • 31
  • 115
  • 231
  • Can't you use the Grid ID directly? Instead of `'<%=RadGrid1.ClientID%>'`? – krlzlx Oct 04 '16 at 13:49
  • That does plug in the actual grid Id like this: var gridSegmentDetail = $find('ctl00_ctl00_GlobalContent_MainContent_ucPriceProtectionSegment_gridSegmentDetailEdit'); I just don't think the grid is output to the page at the time the function is being called. – Blake Rivell Oct 04 '16 at 13:52
  • Ok so what's exactly the error message? – krlzlx Oct 04 '16 at 13:58
  • @krlzlx I updated the post to have the exact error messages. Basically the grid is never being found. I can guarantee it isn't an ID issue, it is that the grid must not be output on the page yet. – Blake Rivell Oct 04 '16 at 14:04
  • Did you read this post: [http://stackoverflow.com/questions/18700698/find-returns-null](http://stackoverflow.com/questions/18700698/find-returns-null) ? – krlzlx Oct 04 '16 at 14:06
  • @krlzlx Thank you for the post, but I have tried both $get and getElementByID and no luck. I can't stand webforms. – Blake Rivell Oct 04 '16 at 14:34
  • @krlzlx actually I lied. document.getElementById does find the grid. But for some reason the .get_batchEditingManager() function is now not available on it. I believe this is why Telerik recommends using $find which cant be used here.. – Blake Rivell Oct 04 '16 at 14:37

1 Answers1

1

I ended up doing the following and it seems to be working. However, not sure how reliable it is.

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "saveCallBack", "setTimeout(SaveGridChanges, 100);", true);

I am assuming setTimeout gives jQuery time to load or whatever $find relies on.

If someone has a better solution, please post.

Blake Rivell
  • 13,105
  • 31
  • 115
  • 231
  • 2
    Use `$(document).ready(function(){SaveGridChanges();});` instead of `setTimeout(SaveGridChanges, 100);`. then u can be sure that the UI is ready – Daniel Pl. Oct 05 '16 at 15:10