THESE FORUMS ARE NOW FROZEN
Please choose "Forums" from the Main menu of www.entityspaces.net to get to our new forums.

esDataSource & DetailsView

rated by 0 users
This post has 11 Replies | 1 Follower

Top 500 Contributor
Posts 8
ssfpridav Posted: 05-08-2009 9:53 AM

I am trying to create a master/detail page to get the hang of using esDataSource. I have the master list (select-only GridView) working well. However, I am having a strange problem with my detail view (DetailsView).

I am trying to get all of the Insert/Update/Delete operations to work. Currently only the Delete works.

When I try to do either Insert or Update the DetailsView goes into the Insert/Update mode but nothing happens when I click the Insert/Update button. It stays in the Insert/Update mode and the esDataSource's esInsert/esUpdate methods don't fire! There is no Exception generated. Am I doing something wrong or does esDataSource not work well with the DetailsView?

Code:
                <es:esDataSource ID="ContactDetailESDS" runat="server"
                    OnesSelect="ContactDetailESDS_esSelect"
                    onescreateentity="ContactDetailESDS_esCreateEntity"
                    onesdelete="ContactDetailESDS_esDelete" 
onesinsert=
"ContactDetailESDS_esInsert" onesupdate="ContactDetailESDS_esUpdate" />
<asp:DetailsView ID="ContactDetails" runat="server"
AutoGenerateRows=
"False" DataKeyNames="Id"
DataSourceID=
"ContactDetailESDS">
<Fields>
...
</Fields>
</asp:DetailsView>
Code:
1        protected void ContactDetailESDS_esSelect(object sender, EntitySpaces.Web.esDataSourceSelectEventArgs e)
2 {
3 Trace.Warn("ContactDetail Loading, selected id = " + (ContactListGV.SelectedValue ?? "-"));
4 5 ContactCollection contact = new ContactCollection();
6 if (ContactListGV.SelectedValue != null)
7 {
8 contact.Query.Where(contact.Query.Id == (int)ContactListGV.SelectedValue);
9 contact.Query.Load();
10 }
11 e.Collection = contact;
12 }
13 protected void ContactDetailESDS_esInsert(object sender, EntitySpaces.Web.esDataSourceInsertEventArgs e)
14 {
15 Trace.Warn("Inserting an entry.");
16 //e.EventWasHandled = true; 17 }
18 protected void ContactDetailESDS_esUpdate(object sender, EntitySpaces.Web.esDataSourceUpdateEventArgs e)
19 {
20 Trace.Warn("Updating an entry.");
21 //e.EventWasHandled = true; 22 }
23 protected void ContactDetailESDS_esDelete(object sender, EntitySpaces.Web.esDataSourceDeleteEventArgs e)
24 {
25 if (e.Entity != null)
26 {
27 Contact contact = (Contact)e.Entity;
28 contact.IsActive = false;
29 contact.Save();
30 ContactListGV.DataBind();
31 ContactDetails.DataBind();
32 e.EventWasHandled = true;
33 }
34 }
35 protected void ContactDetailESDS_esCreateEntity(object sender, EntitySpaces.Web.esDataSourceCreateEntityEventArgs e)
36 {
37 Contact entity = new Contact();
38 if (e.PrimaryKeys != null)
39 {
40 entity.LoadByPrimaryKey((int)e.PrimaryKeys[0]);
41 }
42 else 43 {
44 entity.AddNew();
45 }
46 e.Entity = entity;
47 }

 
Top 10 Contributor
Posts 905

Please see this sample and compare your code

http://community.entityspaces.net/forums/thread/3458.aspx

Regards, Scott Schecter EntitySpaces | Blog | Twitter

Top 500 Contributor
Posts 8

Scott, that code uses a FormView rather than a DetailsView. In fact, as noted here http://community.entityspaces.net/forums/thread/10474.aspx, there aren't any examples or documentation that use the DetailsView. Is there a known issue with the DetailsView?

In comparing the code the biggest difference is the FormView vs DetailsView. There aren't any functional differences in the relevant code (the esSelect and esCreateEntity functions). I am implementing a few more of the esDataSource's events than that example and I haven't implemented the ItemInserted/ItemUpdated events of the DetailsView. But those differences wouldn't cause the problem I am seeing.

Top 10 Contributor
Posts 905
The only differences in the Form and Detail view controls is that one or the other (I think it's the form view) can be templated, so the sample I pointed you to is still valid. Operations should be the same...

Regards, Scott Schecter EntitySpaces | Blog | Twitter

Top 500 Contributor
Posts 8

Scott, I ran a little test to see what would happen when I used both the DetailsView and a FormView attached to the same esDataSource control. I found that without any changes to my code the FormView worked just fine while the DetailsView continued to show the same behaviour. I added handlers for the DetailsView's Inserting and Updating events to see if they fire but they do not. So something about the DetailsView bound to an esDataSource prevents the DetailsView from even trying to execute on a requested Insert or Update.
 

Top 10 Contributor
Posts 905
I will create a small sample over the weekend

Regards, Scott Schecter EntitySpaces | Blog | Twitter

Top 10 Contributor
Posts 905

This works for Create, Read, Update, and Delete. The only issue I haven't worked out in this sample is having the detail view correctly refresh everytime, but this should give you a place to start

 

Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Sandbox.Web.Default"
    Async="true" %>

<%@ Register Assembly="EntitySpaces.Web" Namespace="EntitySpaces.Web" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="EmployeeID"
            DataSourceID="EsDataSource1" AllowPaging="True" AllowSorting="True"
            oniteminserted="DetailsView1_ItemInserted" 
            onitemupdated="DetailsView1_ItemUpdated" 
            onitemdeleted="DetailsView1_ItemDeleted">
            <Fields>
                <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False"
                    ReadOnly="True" SortExpression="EmployeeID" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="TitleOfCourtesy" HeaderText="TitleOfCourtesy" SortExpression="TitleOfCourtesy" />
                <asp:BoundField DataField="BirthDate" HeaderText="BirthDate" SortExpression="BirthDate" />
                <asp:BoundField DataField="HireDate" HeaderText="HireDate" SortExpression="HireDate" />
                <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                <asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" />
                <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
                <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
                <asp:BoundField DataField="HomePhone" HeaderText="HomePhone" SortExpression="HomePhone" />
                <asp:BoundField DataField="Extension" HeaderText="Extension" SortExpression="Extension" />
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
        <cc1:esDataSource ID="EsDataSource1" runat="server" AutoPaging="true" AutoSorting="true"
            OnesCreateEntity="EsDataSource1_esCreateEntity" 
            OnesSelect="EsDataSource1_esSelect"  />
    </div>
    </form>
</body>
</html>
 
Code:
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Web.UI;
using BusinessObjects;
using Elmah;
using EntitySpaces.Core;
using EntitySpaces.Interfaces;

namespace Sandbox.Web
{
    public partial class Default : Page
    {
        protected void Page_Load(object sender, EventArgs e){}

        protected void EsDataSource1_esSelect(object sender, EntitySpaces.Web.esDataSourceSelectEventArgs e)
        {
            var employees = new EmployeesCollection();
            employees.Query.OrderBy(employees.Query.EmployeeID.Ascending);

            e.Collection = employees;
        }

        protected void EsDataSource1_esCreateEntity(object sender, EntitySpaces.Web.esDataSourceCreateEntityEventArgs e)
        {
            var employee = new Employees();
            if (e.PrimaryKeys != null)
            {
                employee.LoadByPrimaryKey((int)e.PrimaryKeys[0]);
            }

            e.Entity = employee;
        }

        protected void DetailsView1_ItemInserted(object sender, System.Web.UI.WebControls.DetailsViewInsertedEventArgs e)
        {
            DetailsView1.DataBind();
        }

        protected void DetailsView1_ItemUpdated(object sender, System.Web.UI.WebControls.DetailsViewUpdatedEventArgs e)
        {
            DetailsView1.DataBind();
        }

        protected void DetailsView1_ItemDeleted(object sender, System.Web.UI.WebControls.DetailsViewDeletedEventArgs e)
        {
            DetailsView1.DataBind();
        }
    }
}

Regards, Scott Schecter EntitySpaces | Blog | Twitter

Top 500 Contributor
Posts 8

Scott, Thanks for your help. I'll try to start fresh again with a minimalistic approach like what you have here. Is there any chance the database version could be at issue? My database is SQL 2000 so I can't rely on the auto-paging, auto-sorting, or auto-loading...

 

Top 10 Contributor
Posts 905

Ahhh that might be it, when you are not using the AutoPaging of the esDataSrouce you must load the query prior to assigning it to e.Collection. If you were using AutoPaging then you would not call the .Load(). So here is a sample esSelect event when AutoPaging is off

 

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObjects;

namespace Sandbox.Web
{
    public partial class ListControlWithDataSource : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e){}

        protected void EsDataSource1_esSelect(object sender, EntitySpaces.Web.esDataSourceSelectEventArgs e)
        {
            var employees = new EmployeesCollection();
            employees.Query.OrderBy(employees.Query.LastName.Ascending);
            if (employees.Query.Load())
            {
                e.Collection = employees;
            }
        }
    }
}
 

Regards, Scott Schecter EntitySpaces | Blog | Twitter

Top 500 Contributor
Posts 8

I was able to get a very simple test case to work. Now I just need to figure out what it is about my previous setup that is causing problems. Working DetailsView code is below.

 

Code:
<es:esDataSource ID="RoleESDS" runat="server" LowLevelBind="True" 
    onescreateentity="RoleESDS_esCreateEntity" onesselect="RoleESDS_esSelect" />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
    DataKeyNames="Id" DataSourceID="RoleESDS" Height="50px" Width="125px">
    <Fields>
        <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" 
            ReadOnly="True" SortExpression="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:BoundField DataField="Description" HeaderText="Description" 
            SortExpression="Description" />
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
            ShowInsertButton="True" />
    </Fields>
</asp:DetailsView>
 
Code:
1        protected void Page_Load(object sender, EventArgs e)
2        {
3        }
4        protected void RoleESDS_esSelect(object sender, EntitySpaces.Web.esDataSourceSelectEventArgs e)
5        {
6            Trace.Warn("esSelect()");
7            var roles = new RoleCollection();
8            roles.Query.OrderBy(roles.Query.Name.Ascending);
9            if (roles.Query.Load())
10           {
11               e.Collection = roles;
12           }
13       }
14       protected void RoleESDS_esCreateEntity(object sender, EntitySpaces.Web.esDataSourceCreateEntityEventArgs e)
15       {
16           Trace.Warn("esCreateEntity()");
17           Role role = new Role();
18           if (e.PrimaryKeys == null)
19           {
20               role.AddNew();
21           }
22           else
23           {
24               role.LoadByPrimaryKey((int)e.PrimaryKeys[0]);
25           }
26           e.Entity = role;
27       }
Top 500 Contributor
Posts 8

I finally found the problem. I had somehow gotten the DetailsView's command field to have its ButtonType set to Image but hadn't configured any images. This somehow breaks the DetailsView's Update and Insert operations. Providing valid images or switching it to another ButtonType resolves the issue.

Scott, thanks for all your help! 

Top 10 Contributor
Posts 905
Excellent work! Glad you got your issue sorted out.

Regards, Scott Schecter EntitySpaces | Blog | Twitter

Page 1 of 1 (12 items) | RSS
Copyright © 2005 - 2009, EntitySpaces, LLC