DetailsView is not visible when there is no data in DataSource
Asked Answered
C

1

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

Cynicism answered 20/8, 2010 at 12:13 Comment(0)
P
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>
Picked answered 20/8, 2010 at 12:18 Comment(9)
Thansk @Ben, could you give an example of the template with an "Insert" command?Cynicism
I have updated my simple example. THe button should turn the DetailsView into Insert mode.Picked
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...Cynicism
Got it, the CommandName must be "New". Thanks, answered it is.Cynicism
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.Hibbs
@Hibbs Do you have any TextBox controls in your InsertItemTemplate? They don't magically appear you have to add them.Picked
@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.Hibbs
@Timwi, it just depends whether you are using templatefields or boundfields.Picked
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.Picked

© 2022 - 2024 — McMap. All rights reserved.