CslaDataSource does not contain items

CslaDataSource does not contain items

Old forum URL: forums.lhotka.net/forums/t/8663.aspx

halcwb posted on Monday, March 15, 2010

I am struggling setting up a web interface using a regular ASP.NET project. I am trying to follow the instructions in the book. So I have a very simple business library with security set up. I have created the ASP.NET and managed to get the login functionality working. I also have set up a CslaDataSource pointing to a business object. This datasource is used by a DetailsView. The properties of the BO show up fine in the designer, but when I run the page in IIS, the DetailsView remains empty. I use the following code behind the view page:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using SimpleFormularium.Library;

namespace SimpleFormularium.ASPNET
    public partial class ApotheekEdit : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            if (Session["currentObject"] == null)
                Session["currentObject"] = Apotheek.GetApotheek(1);
                ApotheekEditView.DefaultMode = DetailsViewMode.Edit;

                //if (Session["currentObject"] == null) throw new Exception("Dammit");
                //if (ApotheekEditView.Rows.Count == 0) throw new Exception("Damm: " + ((SimpleFormularium.Library.Apotheek)Session["currentObject"]).Naam + ApotheekEditView.DataItemCount);
                //this.ApotheekEditView.Rows[0].Visible = true;
                if (ApotheekEditView.Rows.Count == 0) ApotheekEditView.DefaultMode = DetailsViewMode.Insert;


Excuse the curses, gotten  a bit frustrated. Anyway, if I uncomment the exceptions, the object has just loaded fine, only the datasource seems to remain empty.

Must be something very stupid and simple as these mistakes are the hardest to find.

Any help will be greatly appreciated.

RockfordLhotka replied on Tuesday, March 16, 2010

The data source control has events you must handle. At the very least you need to handle the select event (SelectObject I think?), and in that event is where you call the business object factory method to get the business object and return it:

e.BusinessObject = Customer.GetCustomer(123);

or something like that.

halcwb replied on Tuesday, March 16, 2010

First of all, thank you  very much for taking the time to answer such a newbie like me.

Unfortunately, that did not work yet. Maybe I got the set up wrong. The control shows up like a blank white square, no fields or labels. So, I am not sure it's the code.

Regards -- Casper

halcwb replied on Tuesday, March 16, 2010

By the way, my intention was to load a default object, so if I use the select event, this is not happening upon loading of the page. In short, I want to have a root business object visible for editing right at the start of opening the web page.

RockfordLhotka replied on Tuesday, March 16, 2010

Web Forms data binding dictates how this works, and you pretty much have to conform to the way Microsoft has this set up.

When the form loads, the form binds. When the form binds it asks the data source control for data (this is true for any data source control, not just CslaDataSource). When the data source needs data it does something to get data. The CslaDataSource raises the select object event to get its data.

So in the end, you must handle that event and set e.BusinessObject to a value - this is the way it works.

halcwb replied on Tuesday, March 16, 2010

I'm sorry to keep bothering you, but I did implement a handler for the select object event to get the data like:

        protected void CslaDataSource1_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
            e.BusinessObject = Apotheek.GetApotheek(1);
            FormView1.DefaultMode = FormViewMode.Edit;
            Page.Title = "Event fired";


However, the problem is, this method is never run as I do not seen the page title change.

I must be doing something very stupid. I also tried to set a very basic standalone webform to test the function of the datasource. Same problem though.

Jerrycurl replied on Tuesday, March 16, 2010

Did you set the OnSelectObject property of the CslaDataSource to be "CslaDataSource1_SelectObject"?

halcwb replied on Tuesday, March 16, 2010

Thank you so kindly!! Visual Studio is a great tool, but sometimes these things are so hidden. I have checked the properties page of the datasource many times, just know I discovered the events tab. I think I have got it.

Copyright (c) Marimer LLC