Persisting Row Selection in Data Controls

While exploring the new features in ASP .Net 4.0, I discovered that Microsoft has introduced a new property in ASP .Net 4.0 for data bound control like GridView,ListView named as EnablePersistedSelection.

In versions prior to Asp .Net 4.0, if a row is selected in Gridview  then navigating to the next page, same row number was get selected automatically. This was because of row selection on basis on index value. But with ASP .Net 4.0, data Key based row selection is also supported. So now by enabling data key based row selection (by making EnablePersistedSelection="True" and assigning DataKeyNames), if a row is selected on page 1, later you move to next page, no row is selected on the next page. On returning back to page 1, old row is still selected.

<asp:ListView ID="lvtopCustomers" runat="server" EnablePersistedSelection="True" DataSourceID="dsCust" DataKeyNames="Custid">

<asp:GridView ID="gvtopCustomers" runat="server" EnablePersistedSelection="True" DataSourceID="dsCust"  DataKeyNames="Custid">

Don't forget to add the DataKeyNames when you enable this property, since the property is fully based on the DataKeyNames associated with the row.

Even though it's a simple feature, it adds more clarity to the selection based grid functionality in a web page.

Comments

Popular posts from this blog

Data Bound Controls in ASP.Net - Part 4 (FormView and DetailsView controls)

ASP.net: HttpHandlers

The Clickjacking attack and X-Frame-Options