Cannot get GUID ID from CSLADataSource

Cannot get GUID ID from CSLADataSource

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


miloy001 posted on Tuesday, July 31, 2007

Briefly, I added CSLADatasource to version 2.0, i have figured out the soring and paging based on some posts here and CSLA 2.1 handbook. I have to use a CustomerID fields on the following event and have to make CustomerID VISIBLE + EDITABLE:

Protected Sub CslaDataSource1_UpdateObject(ByVal sender As Object, ByVal e As BOBases.Web.UpdateObjectArgs) Handles CslaDataSource1.UpdateObject

CID = e.Kays("CustomerID").ToString()
obj = MyList.GetItemByID(CID)
Data.DataMapper.Map(e.Values, obj
)
...

This works but CustomerID should not be editable, i.e. only editable field is in the collection of Keys. How do I get the obj GUID ID or a Field with Visible=False form these CSLADatasource events?

 

RockfordLhotka replied on Tuesday, July 31, 2007

There are a lot of changes/fixes from 2.0 to 2.1.4 (or 3.0) with the CslaDataSource, so if you are using it I suggest upgrading to the current framework version.

However, your specific question has less to do with CslaDataSource than with ASP.NET data binding itself.

The values in UpdateObjectArgs come from data binding, and data binding gets the values from the state of the page itself. If the value isn't on the page, it can't be in the state and so data binding can't provide it through UpdateObjectArgs. That's just the way ASP.NET data binding works.

So if you have fields you don't want displayed on the UI you must either put them into the UI in non-visible fields (zero size columns or whatever) so they are in the DetailsView/FormView/GridView. Or you must reload them from the database.

Remember that the web server is stateless, so the state exists either in the page or in the database.

miloy001 replied on Wednesday, August 01, 2007

I understand what you said. But my issue is:

I have Customer and CustomerList objects, on the select object event, CustomerList is assigned to csladtasource1:

e.BusinessObject = objList     OR
e.BusinessObject = New BOBases.SortedPagedBindingList(Of Customer)(objList, e)

That works but on Update event - Protected Sub CslaDataSource1_UpdateObject(...

How do I know which CustomerBeingUpdated in the CustomerList to pass to the DataMapper.Map?

Data.DataMapper.Map(e.Values, CustomerBeingUpdated)

I know the Customer.GetIdValue plays a roll here, but how/when do I setup the GetIdValue to databinding or CSLADataSource1?  I useed the code sample in the Tracker project but it always said the CustomerBeingUpdated is null. What do I missed?

 

RockfordLhotka replied on Wednesday, August 01, 2007

This is discussed in Chapter 10, and is a common ASP.NET scenario. You need to tell the GridView, DetailsView or FormView which properties of your data are the key properties. There's a property on those controls where you set the key property name(s). The property is DataKeyNames and is discussed early in the chapter during the creation of the RolesEdit.aspx page.

miloy001 replied on Wednesday, August 01, 2007

Got it and thanks!
Dim item = objList.ItemByGUID(GridView1.DataKeys(GridView1.EditIndex).Value)

One suggestion: 

A simple test project using Northwind database so that users can simply replace connection string to run CSLA sample. I know some of developers have troubles or no patiences to setup/run your test sample and just forget it. Your book is great but how many users read it from A to Z?

 

Copyright (c) Marimer LLC