Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Add records to detailsview EmptyDataTemplate

Posted on 2010-09-20
7
Medium Priority
?
1,242 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

886 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