Skip to main content

Command Palette

Search for a command to run...

CSLA.Net: Step-by-Step – Web forms Episode II: The Update strikes back.

Updated
3 min read
CSLA.Net: Step-by-Step – Web forms Episode II: The Update strikes back.

Welcome back! See I told you, you needn’t wait another 2 months for the next installment.

Today, we’ll have a look at updating an existing customer and adding a new customer via our web interface. I would like to mention that, these tutorials barely touch the surface of the CSLA.Net framework. To fully grasp the framework, buy Rockford Lhotka’s book Expert VB.Net Business Object, the value you’ll get by reading the book will definitely be worth the cost.

Also, to get a more detailed look at how to do things, be sure to check out the ProjectTracker project that comes with the framework.

Right, with that out of the way, let’s get started. Open the NorthwindWEB project; we created in the last post. Select the detailsview control, in my case it has the very original name of DetailsView1, and click on the little arrow top-right. Tick the Enable Inserting and Enable Editing checkboxes. It should look something like this:

![clip_image002](http://www.mythicalmanmoth.com/wp-content/uploads/2009/10/clip-image002-thumb.jpg "clip_image002")

You’ll notice that the DetailsView control now has 2 hyperlinks entitled Edit and New

![image](http://www.mythicalmanmoth.com/wp-content/uploads/2009/10/image-thumb10.png "image")

You’ll notice that when you click on either of the links, it automatically changes the detailsview to edit mode and the hyperlinks’ text to either Update or Insert, depending on which link you clicked:

![image_3](http://www.mythicalmanmoth.com/wp-content/uploads/2009/10/image-3-thumb2.png "image_3")

I’m just going to show you the quick and dirty way, managing your application state and object persistence is up to you.

Let’s first look at updating your customer. Open up your code view and add the following code:

Protected Sub dsCustomer_UpdateObject(ByVal sender As Object, ByVal e As Csla.Web.UpdateObjectArgs) Handles dsCustomer.UpdateObject Csla.Data.DataMapper.Map(e.Values, myCustomer) myCustomer.Save() End Sub

Remember we declared our Customer object, earlier. As soon as you click on update your changes will be saved. Now that was easy, wasn’t it?

Now for the Insert. In the global.asax file I’ve added the following in the Session_Start sub, this is to ensure I can persist the current object throughout the application:

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim myCustomer As Northwind.Library.Customer = Northwind.Library.Customer.GetCustomer(3) Session.Add("CustomerObject", myCustomer) End Sub

Next in the DetailsView’s ItemCommand Sub I’ve added the following:

Protected Sub DetailsView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewCommandEventArgs) Handles DetailsView1.ItemCommand If e.CommandName = "New" Then myCustomer = Customer.NewCustomer Session("CustomerObject") = myCustomer End If End Sub

and then in the datasource’s InsertObject sub add the following:

Protected Sub dsCustomer_InsertObject(ByVal sender As Object, ByVal e As Csla.Web.InsertObjectArgs) Handles dsCustomer.InsertObject myCustomer = Session("CustomerObject") Csla.Data.DataMapper.Map(e.Values, myCustomer, "CustomerID") myCustomer.Save() End Sub

So what’ll happen is that as soon as you click on the new link. A Customer.NewCustomer Object will be save in your Session’s CustomerObject. The as the page reloads, it shows the Customer object stored in the session. This object is then saved to the database.

I hope this made sense, it certainly is a quick and dirty way of doing it. Check the Project Tracker’s web project to see a proper implementation.

Until next time..cheerio.

More from this blog

M

Mythical Man Moth

80 posts

Hi, I’m Pieter van der Westhuizen. I'm a professional freelance web & mobile developer from South Africa that has been code slinging for more than 23 years. https://youtube.com/shorts/aCCKAnDNrzM

CSLA.Net: Step-by-Step – Web forms Episode II: The Update strikes back.