DetailsView Control in ASP.Net: Part 3

Introduction

In Part2 of this article series we have discussed how to display Fields with DetailsView Control. Now in this article we will discuss how display Empty Data with DetailsView Control.

Displaying Empty Data with DetailsView Control

The DetailsView control includes two properties that we can use to display a message when no results are returned from its data source. We can use the EmptyDataText property to display an HTML string or the EmptyDataTemplate property to display more complicated content.

Using EmptyDataText Property

The example given below does not return a record because no record in the Product database table has an ID of 12.
 

EmptyDataText_Property-in-DetailView.gif  

<%@ 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"
        EmptyDataText="<b>No Data Found.</b>"
        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] WHERE ID=12">
        </asp:SqlDataSource>

      </div>
    </form>
</body>
</
html>

When we open the page given above, the contents of the EmptyDataText property are displayed.

Using EmptyDataTemplate Property

If we need to display more complicated content when no results are returned, such as ASP.NET controls, then we can specify an EmptyDataTemplate. The page given below illustrates how we can use the EmptyDataTemplate to display complicated HTML content.

EmptyDataTemplate_Property-in-DetailsView.JPG
 

<%@ Page Language="VB" %>

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

    <style type="text/css">
        .noMatching
        {
            background-color: Gray;
            padding:10px;
            font-family:Arial,Sans-Serif;
        }
        .noMatching h1
        {
            color:Lime;
            font-size:16px;
            font-weight:bold;
        }
    </style>

<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"
        Runat="server">
        <EmptyDataTemplate>
        <div class="noMatching">
            <h1>No Data Found.</h1>
            Please select a different record.
        </div>
        </EmptyDataTemplate>
    </asp:DetailsView>

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

       </div>
    </form>
</body>
</
html>

Note: Continue in Next Part.


Similar Articles