Display message using EmptyDataTemplate in the DetailsView Control

Many a times we may want to display a message when there are no records or no matching records found based on a given condition. Using the DetailsView control we can display a message conveying that there are no records.

The DetailsView control includes two properties that can be used to display the appropriate message. To display an HTML string we can use the EmptyDataText property and to display a more complicated message we use the EmptyDataTemplate property.

In this example, the SqlDataSource does not return a record because no records exist in the sample database which has a code 'Code10'. When we display the page, the content in the EmptyDataText property is displayed.

<asp:DetailsView ID="DetailsView1" runat="server"  AutoGenerateRows="true"
DataKeyNames="Code" DataSourceID="MyDataSource"    
EmptyDataText="No Records" 
AutoGenerateInsertButton="true"   
AutoGenerateEditButton="true">
</asp:DetailsView>

<asp:SqlDataSource ID="MyDataSource"  
ConnectionString="<%$Connectionstrings:ERPConnectionString%>"
SelectCommand="SELECT * FROM Sample WHERE Code='Code10'" 
UpdateCommand="Update SAMPLE SET Name=@Name,description=@description Where Code=@Code"
DeleteCommand="Delete SAMPLE Where Code=@Code" runat="server"/>


We can also specify an EmptyDataTemplate if we want to display more complicated content using ASP.Net controls. The below code demonstrates how to use EmptyDataTemplate in the DetailsView control.

<head runat="server">

<style type="text/css">
.noRecords
{
  background-color:Blue;
  padding:10px;
   font-family:WP Greek Courier;
}

</style>
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DetailsView ID="DetailsView1" runat="server"  AutoGenerateRows="true"
        DataKeyNames="Code" DataSourceID="MyDataSource"     
        AutoGenerateInsertButton="true"   
        AutoGenerateEditButton="true">
        <EmptyDataTemplate>
        <div class="noRecords">
          <h1>No records </h1>
         </div>
        </EmptyDataTemplate>
    </asp:DetailsView>
<asp:SqlDataSource ID="MyDataSource"  
ConnectionString="<%$Connectionstrings:ERPConnectionString%>"
SelectCommand="SELECT * FROM Sample WHERE Code='Code10'" 
UpdateCommand="Update SAMPLE SET Name=@Name,description=@description Where Code=@Code"
DeleteCommand="Delete SAMPLE Where Code=@Code" runat="server"/>

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


output of DetailsView Control with EmptyDataTemplate