Solved

Add records to detailsview EmptyDataTemplate

Posted on 2010-09-20
7
1,197 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

937 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

5 Experts available now in Live!

Get 1:1 Help Now