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.
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.
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>
DetailsView Articles
- Difference between DetailsView and FormView control
- DataBinding a DetailsView control
- GridView and DetailsView Master/Detail page using SqlDataSource control
- GridView and DetailsView master detail page using ObjectDataSource control
- DropDownList and GridView Master/Detail page using ObjectDataSource control
- DetailsView databound event
- DetailsView integer type conversion error?
- Using detailsview control datakeynames property
- How to get the datakey value in DetailsView control
- DetailsView autogeneraterows property
- DetailsView fields
- Formatting DetailsView control with style properties
- Using Boundfields in DetailsView control
- How to access DetailsView's fields programmatically
- Bind a DetailsView control with a DropDownList control
- Using Command buttons in DetailsView control
- Using Commandfield element in a DetailsView control
- Using buttonfield in a DetailsView control
Most Viewed
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
ERP
ERP Software Development
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls