1

I have a GridView called myGrid, I am retrieving some list items and adding them to GridView but I have set the rowlimit of my GridView to 10. So when the page is loaded, the myGrid comes with 10 rows. which looks like this:

GridRows

When I click at View next ten comments through an AJAX call to a [WebMethod] I am appending myGrid with next 10 items with javascript, which looks like this:

enter image description here

But now when I click at Proccess button it calls an C# function and I am iterating throughout all grid rows with following code:

protected void Proccess_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in myGrid.Rows)
    {
           //TODO logic
    }
}

when button is clicked I can see only 10 rows that were added first, which means rows added with ajax function(javascript) are not being shown to process.

2 Answers 2

1

Javascript is purely client side and C# will not be able to see changes via javascript. you would need to add the rows not with javascript but to the GridView and have AJAX refresh the page.

2
  • Is there anyway to read all rows that were added with javascript. Oct 27, 2017 at 14:10
  • You can dive into the dom and read all the <tr> tags should be something close to document.getElementsByTagName('tr').length, but if you want to only specify those that were added just from javascript and not from c#, that may be a little more difficult.
    – Mike
    Oct 27, 2017 at 16:54
1

This issue is because of ViewState of the control.

Any changes you make Client Side doesn't get added to the ViewState and thus during post back that data is not sent to the server.

Two options to resolve this

  1. Get rid of the ajax call and use the PostBack to load and append that next 10 items
  2. When process is clicked first using jQuery read the items on the Grid and put it on a Hidden control and read it from the server click event.
6
  • I can append the next 10 rows only with ajax, I cannot call with anything else. Is there anyway to manipulate with ViewState to make it posible to process? Oct 27, 2017 at 14:09
  • Nope ViewState modification is all with the Server and its not wise to change that.. you can try the next option 2... add a ClientButtonClick function and use JavaScript Oct 27, 2017 at 14:10
  • hmm I did not understand totally the second option. You are saying with jquery to get all grid Rows and add to a hidden control and then process with server side? right? Oct 27, 2017 at 14:16
  • Yes. make sure the Hidden control is set to runat=sever and also take a look at how to read server side controls id in JavaScript Oct 27, 2017 at 14:18
  • <%=control.ClientID%> is the way to read ID, but how to read items from server side when we are adding rows with javascript Oct 27, 2017 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.