views:

24

answers:

1

Hi guys.

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

+2  A: 

You need to add an EmptyDataTemplate:

   <asp:DetailsView ID="MyDV" runat="server">
        <EmptyDataTemplate>
            <asp:Button ID="InsertButton" runat="server" CommandName="Add" />
        </EmptyDataTemplate>
   </asp:DetailsView>
Ben Robinson
Thansk @Ben, could you give an example of the template with an "Insert" command?
Wodzu
I have updated my simple example. THe button should turn the DetailsView into Insert mode.
Ben Robinson
@Ben 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
Got it, the CommandName must be "New". Thanks, answered it is.
Wodzu