Solved

Add records to detailsview EmptyDataTemplate

Posted on 2010-09-20
7
1,221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

622 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