7

What to do to show DetailsView even when there is no data in underlying datasource?

The problem is that when DetailsView is not visible, the command buttons are also not visible. So I can not insert a new record to a database. I would like to have always visible DetailsView with its DefaultMode set to ReadOnly.

If it is not possible I would like to ask for an workaround for this problem.

One workaround which I have on my mind is to check if datasource have any records. If no then programmaticaly change DefaultMode to Insert. However this causes another problem - how to count the number of records in DataSource.

All my problems are related to the ASP 3.5

Wodzu
  • 6,932
  • 10
  • 65
  • 105

1 Answers1

12

You need to add an EmptyDataTemplate:

   <asp:DetailsView ID="MyDV" runat="server">
        <EmptyDataTemplate>
            <asp:Button ID="InsertButton" runat="server" CommandName="New" />
        </EmptyDataTemplate>
   </asp:DetailsView>
Timwi
  • 65,159
  • 33
  • 165
  • 230
Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
  • Thansk @Ben, could you give an example of the template with an "Insert" command? – Wodzu Aug 20 '10 at 12:26
  • I have updated my simple example. THe button should turn the DetailsView into Insert mode. – Ben Robinson Aug 20 '10 at 12:49
  • the button is visible, but after pressing it DetailsView doesn't change its Mode. However the page is refreshing. Do I need to specify the command myslef? (After browsing the web I've found that the proper name for the command is "Insert". However, now I am getting message that the DetailsView must be in insert mode... – Wodzu Aug 20 '10 at 13:08
  • Got it, the CommandName must be "New". Thanks, answered it is. – Wodzu Aug 20 '10 at 13:21
  • This still doesn’t work. If I click the “New” button, I only get an “Insert” and “Cancel” link, but no textboxes to enter anything into. – Timwi Feb 02 '12 at 16:48
  • @Timwi Do you have any TextBox controls in your InsertItemTemplate? They don't magically appear you have to add them. – Ben Robinson Feb 02 '12 at 18:06
  • @BenRobinson: Why do I have to add them *only when there is no row in the database table*, but in all other cases they *do* magically appear? That’s just crap design. – Timwi Feb 05 '12 at 21:02
  • @Timwi, it just depends whether you are using templatefields or boundfields. – Ben Robinson Feb 06 '12 at 09:43
  • I was just pointing out that when you use templatefields you need to provide a template for each mode otherwise that mode will just be blank where if you use bound fields it magically happens. – Ben Robinson Jan 01 '13 at 12:03