Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
Adding rows to WebDataGrid

I have a very simple ASP.NET web application with a single WebDataGrid component. I did NOT bind the grid to any kind of DB object, but I did initialize it within the page load process (in function "Page_Load").
DataTable dt = new DataTable();
WebDataGrid1.DataSource = dt;
To add new lines to the WebDataGrid every 10 seconds.
I have a javascript function which is doing an ajax request using the "PageMethods" technique every 10 seconds. After the Ajax returns, the OnSuccess function is executed. I iterate through the JSON object (the "Response" parameter of the 'OnSuccess' function) and trying to add the rows values that I got from the server into the grid using the add function.
function OnSuccess(Response)
      var grid = $find("<%= WebDataGrid1.ClientID %>");
      var rows = grid.get_rows();
      for(var i=0;i<3;i++)
            arr = new Array();
            arr[0] = Response[i].Name;
            arr[1] = Response[i].Age;
            arr[2] = Response[i].Country;
            arr[3] = Response[i].***;
I could not add more than one row.
During the second iteration the 'rows.add(arr)' line, raise an exception "this.get_grid() is null", Also I have notice that the page is trying to load itself again, or trying to do something else, I'm not sure. In any case, I could NOT add more then one single row.
Is there any way that I can add more then one row into the grid in the client-side using an Ajax request?
Why the 'rows.add(arr)' function raise an exception after the first iteration?
  • 14049
    Offline posted

    The reason why you are not able to add more than one row is that you create a new DataTable on every post back and the new row is added to it. I understand that this is for testing purposes and you don't have an actual database running. So to test if this works you would need to operate the same DataTable on post backs. The DataTable that you create on the first request should be put into the session, and then on subsequent postbacks it needs to be restored from the session. That way you will be adding to one DataTable, not to a new one every time.

    Regarding exception. I am not seeing how you connect OnSuccess event handler, but it probably needs to be reassigned to the grid that returns from the callback on every callback.

    Hope this helps.