Solved

Add records to detailsview EmptyDataTemplate

Posted on 2010-09-20
7
1,202 Views
Last Modified: 2012-08-14
Hi, I have a details view on my page and this is filtered on a dropdown selection box.
My issue is that not all selections produce records to display in the grid view and so I have a EmptyDataTemplate instead.
What I would like to know is how you go about setting up the EmptyDataTemplate to add a new record to the dataset.
0
Comment
Question by:Simon Cripps
  • 4
  • 3
7 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 33717032
--->My issue is that not all selections produce records to display in the grid view and so I have a EmptyDataTemplate instead.

Wondering, you want it for DetailsView or GridView?

Here is a sample that talks about that using GV but the concept should remain more or less the same with DetailsView:
http://www.aspdotnetfaq.com/Faq/How-to-insert-row-in-GridView-with-SqlDataSource.aspx

0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33717072
If it is DetailsView you can Check if  there is no data then change it's Mode to Insert. I am not sure why would you want to use EmptyDataTemplate.
What I mean is something like this:

protected void DetailsView1_DataBound(object sender, EventArgs e)
{
        if(DetailsView1.DataItemCount==0){
            DetailsView1.ChangeMode(DetailsViewMode.Insert);
        }
}
0
 

Author Comment

by:Simon Cripps
ID: 33718013
Sami,
Thanks yourway looks like the route I should be going.
I have applied the following VB code, which is a conversion of yours and it goes to insert mode for all records even ones that have a record you view, any reasons why

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
             DataSourceID="SqlDataSource5"  AllowPaging="True" OnDataBound="DetailsView1_DataBound" >


Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As EventArgs)

        If DetailsView1.DataItemCount = 0 Then
            DetailsView1.ChangeMode(DetailsViewMode.Insert)
        End If
    End Sub

0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 41

Expert Comment

by:guru_sami
ID: 33718105
Did you set the breakpoint and see if DetailsView1.DataItemCount is always coming out to 0?
Can you add this else statement:
If DetailsView1.DataItemCount = 0 Then
            DetailsView1.ChangeMode(DetailsViewMode.Insert)
Else
      DetailsView1.ChangeMode(DetailsViewMode.ReadOnly)
        End If
0
 

Author Comment

by:Simon Cripps
ID: 33720170
I realy can't work this out I have looked at the break point output and yes it is always coming out to zero . This therefor implies there are no records, however I have a datalist that is using the same data source that does show the data where these is some.

<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource5">

 <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True"
           AutoGenerateRows="False" DataSourceID="SqlDataSource5" Height="50px"
           Width="125px" OnDataBound="DetailsView1_DataBound" >


 Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        If DetailsView1.DataItemCount = 0 Then
            DetailsView1.ChangeMode(DetailsViewMode.Insert)
        Else
            DetailsView1.ChangeMode(DetailsViewMode.Edit)
        End If
    End Sub

For cases where there is a record it goes into Insert mode as well, if I the cancel the insert in the details veiw at this point I see the existing record in update mode.
This now implies that there was a record however  DetailsView1.DataItemCount  does not work, any ideas?
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 33720619
Ok so you might be databinding your DV somewhere in code-behind correct, when the DropDownSelection changes?
I believe it will be DropDownSelectedIndexChanged....
Try to explicitly change the mode to Edit in there like:

'When ddl selected Index changes
DetailsView1.ChangeMode(DetailsViewMode.Edit)
DetailsView1.DataBind()

I think what is happening is the DV is getting stuck to Insert mode and so DataItemCount will be zero all the time.
0
 

Author Closing Comment

by:Simon Cripps
ID: 33723306
Spot on with the answers, many thanks for all you help.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question