Solved

Add records to detailsview EmptyDataTemplate

Posted on 2010-09-20
7
1,186 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now