I am not able to get paging to work correctly in the webdatagrid.
I have a new blank ASP.NET project and have just a webdatagrid control. I create a simple datatable with 1 column and 10 rows in the code-behind and set the grid's datasource to the datatable's DefaultView object in the Page_Load() method.
- PageSize = 2
- PagerMode = Numeric
- Ajax is enabled
- Auto generate columns is NOT enabled
When I run the project, the grid initially displays as expected with the 1st two rows, and the numeric pager at the bottom with 5 page link buttons. Now, when I click on any of the pager buttons, the grid seems to refresh but still only displays the 1st two rows. It does not display the correct rows.
Can anyone assist in figuring out why this doesn't work?
I'm including my ASPX and ASPX.CS files.
Thank you!
Hello Sanjay,
Thank you for posting in our community.
I have created and attached a small sample illustrating the desired behavior. Please let me know if you need more information.
4048.Sample.zip
I was able to successfully run the test sample. I'm using CLR4.0 controls in my project so I updated the sample accordingly.
After comparing my test with the given sample, I found that I was binding the grid in an "if (!Postback) {}" statement which was causing the paging to not work. This implies that the entire data must be retrieved upon every call to the page; wouldn't this cause performance issues for large amounts of data (which is why we'd be using paging in the first place)?
Thanks
To prevent setting the WebDataGrid's data source on every request, you can use the Session object provided by ASP.NET:
public DataTable GetDataSource() { DataTable dt = new DataTable("Table"); if (!IsPostBack) { dt.Columns.Add("id"); ; DataColumn[] PrimaryKeyColumns = new DataColumn[1]; PrimaryKeyColumns[0] = dt.Columns["id"]; dt.PrimaryKey = PrimaryKeyColumns; for (int i = 0; i <= 10; i++) { dt.Rows.Add(new object[] { i }); } Session["dtData"] = dt; } else { dt = (DataTable)Session["dtData"]; } return dt; }
Looking forward to hearing from you.