Sunday, 8 June 2014

Persisting Row Selection of Data Controls in ASP.NET

Some times we need show large amount of data like hundred of rows but its very difficult to show that in a single web page so we use the paging mechanism to handle this kind of situation. ASP.NET Data Controls row selection feature was based on row index , this of course produce an issue if you try to select an item in the first page then navigate to the second page without select any record you will find the same row selected in the second page( this occurs due to same row index).

For Example:
  • Select the third row in the GridView.
  • Navigate to second page without doing any selection
  • You will find the third row in the second page selected.

I discovered that Microsoft has introduced a new property in ASP .Net 4.0 for data bound control like GridView,ListView named as EnablePersistedSelection. Now you are thinking why EnablePersistedSelection introduced ??

It is a new feature which replace the old selection mechanism which based on row index to be based on the row data key instead.

So now by making EnablePersistedSelection="True", if a row is selected on first page, later you move to next page, no row is selected on the next page but on returning back to first page, old row is still selected.Here is the code for that:-
 <asp:GridView ID="PersistedGridView" runat="server" EnablePersistedSelection="true">
 </asp:GridView>
Don't forget to add the DataKeyNames when you enable this property, since the property is fully based on the DataKeyNames associated with that row.If not, following error occurs:-

That’s it.. Hope it will help you..

Happy Programming...

No comments:

Post a Comment