Skip to main content

Command Palette

Search for a command to run...

CSLA.Net: Step-by-Step – Web forms Episode I

Updated
3 min read
CSLA.Net: Step-by-Step – Web forms Episode I

Welcome to the first CSLA.Net: Step-by-Step for 2008!

It’s been some time since I last posted a new entry, but one of my new year’s resolutions is to post at more frequent intervals.
Right, today we’re going to be looking at using CSLA.Net in a web forms application. The great thing about having all your business logic on a separate layer is that you can use it as-is in your web application.

Ok, let’s get cracking:

First, create a new ASP.Net Website project called NorthwindWEB. Add a reference to Csla.dll and to your Northwind BO dll, in my case it’s called Nwind.Library.dll.If you don’t see a control called CslaDataSource in your toolbox; add a new item and select the Csla.dll file, which should add the CslaDataSource control.

Open the Default.aspx page in design view. Drag a new CslaDataSource control onto the page and rename it dsCustomer. Next, click Configure Data Source; and enter the following in the Business object type field: Nwind.Library.Customer, Nwind.Library. In case you’re wondering the format is <Fully Qualified Type name>, <Type Assembly Name>.

Next, we’ll do a simple test to see if everything is working so far. Add a textbox and button control to your form.Next open the code view of the page and add the following code to the button’s click event:

Dim myCustomer As Customer
myCustomer \= Customer.GetCustomer(3)
TextBox1.Text \= myCustomer.CompanyName

Press F5, and run the application. If everything goes well, you should be able to click the button and the customer’s company name should be displayed in the textbox.
Easy, right? Ok, so we’ve just displayed one customer and we haven’t used the data source , we’ll get to use some cool data binding next.

Drag a DetailsView control onto the page and select dsCustomer as the data source. You can format the DetailsView if you want to make it look pretty.

You’ll notice that if you run the project now, the DetailsView is not visible on the page. That’s because the data source (dsCustomer) it uses are empty at the moment, we need to hook up some events to make it work first. Open up the code view and move the declaration of the myCustomer object up and add the add code to the Page_Load event to load data into the myCustomer object. Also add code to the dsCustomer_SelectObject event. The code should look something like this:

Imports Nwind.Library

Partial Class _Default

Inherits System.Web.UI.Page

Dim myCustomer As Customer

Protected Sub dsCustomer_SelectObject(ByVal sender As Object, ByVal e As Csla.Web.SelectObjectArgs) Handles dsCustomer.SelectObject

e.BusinessObject = myCustomer

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

myCustomer = Customer.GetCustomer(3)

TextBox1.Text = myCustomer.CompanyName

End Sub

End Class

Hit F5 and run your app. You’ll see that it show all the details for the selected customer. How easy was that?

That concludes this episode, tune in next time, where we’ll tackle updating an existing customer and adding a new one, using web forms. And I promise you wouldn’t need to wait another 2 months either to see it 😉

Till later, keep coding!

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