DetailsView Control in ASP.NET - Part 4


Introduction

In Part 3 of this article series we have discussed how to display Empty Data, that is to display the message in case of no data found. Now in this article we will discuss how to Paging through Data in DetailsView Control.


Paging through Data in DetailsView Control

We can use the DetailsView to page through a set of database records by enabling the DetailsView control's AllowPaging property. There are two ways to use the paging in DetailsView Control.

Using PostBack

If we use PostBack then, each time navigation in another page will request to server. The page given below illustrates how we can page through the records in the PRO_LIST database table.



<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    <asp:DetailsView
        id="dtlProducts"
        DataSourceID="SqlDataSource1"
        AllowPaging="true"
        Runat="server" />

        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
            SelectCommand="SELECT [ID], [TITLE], [COMPANY], [PRICE] FROM [PRO_LIST]">
        </asp:SqlDataSource>
   
    </div>
    </form>
</body>
</html>


Using AJAX

By default, when we page through records with the DetailsView control, the page is posted back to the server each and every time we click a page number. As an alternative, we can take advantage of AJAX to page through records. When we take advantage of AJAX, only the DetailsView control and not the entire page are updated when we navigate to a new page of records. AJAX (Asynchronous JavaScript and XML) enables us to retrieve content from a web server without reloading the page. AJAX works with all modern browsers. We have included the date and time above the DetailsView Control and we can check it while navigating to another page, it will not be updated. The page given below illustrates how we can use AJAX with the DetailsView control.



<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div>
    <b><%= DateTime.Now %></b>
    <br />
    <br />
    <br />
    </div>
    <asp:DetailsView
        id="dtlProducts"
        DataSourceID="SqlDataSource1"
        AllowPaging="true"
        EnablePagingCallbacks="true"
        Runat="server" />

        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
            SelectCommand="SELECT [ID], [TITLE], [COMPANY], [PRICE] FROM [PRO_LIST]">
        </asp:SqlDataSource>
   
    </div>
    </form>
</body>
</html>


In above code DetailsView control included an EnablePagingCallbacks property which has the value true. This property enables AJAX while paging with the DetailsView. We have also included current date and time to proof it is not being PostBack. The time is not updated when we navigate to a new page of records. The time is not updated because the entire page is not updated. When we navigate to a new page, only the contents of the DetailsView are updated. 

Note: Continue in Next Part.

Comments

Popular posts from this blog

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples