Tuesday, August 19, 2008

ASP.NET 3.5 ListView Web Control & CSLA

In .NET Framework 3.5, there is this new web control called ListView. ListView has been there in WinForm development for a while, but now it is available for web development. You can think of ListView as like an upgrade to GridView - a super cool upgrade - although GridView is still there. In fact, many of the stuff that ListView has are quite similar to those in GridView.

If you have a DataSet, it is super easy to bind to a ListView - all the available methods will work out of the box. A lot of tutorials on the web are written against this type of example (ListView & DataSet). What if you are using your own collection or list? In this example, I am using a CSLA based list. Hopefully this will be helpful for you in your ASP.NET - CSLA coding endeavor.

Binding
I use a CSLA datasource object. If you are using CSLA in your web project, this should be available for you.

Select the CSLADataSource from the toolbox and drag it into your page/control. You can change its ID and set some properties. Go to the event and implement the onselectobject methods such as this:


protected void CslaDSMyClassList_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
{
e.BusinessObject = GetMyClassList();
}

private Csla.SortedBindingList GetMyClassList()
{
object businessObject = Session[SessionKeys.CurrentObject];
if (businessObject == null || !(businessObject is Csla.SortedBindingList))
{
Csla.SortedBindingList temp = null;
temp = new Csla.SortedBindingList(MyClassList.GetMyClassList());

temp.ApplySort(SortByPropertyName, SortDirection);
businessObject = temp;
Session[SessionKeys.CurrentObject] = businessObject;
}
return (Csla.SortedBindingList)businessObject;
}

Those methods guarantee that your ListView will always binded to a Csla.SortableBindingList holding your list, therefore this will Sort a feasible feature in our ListView with just some simple minimal coding.

Next, select a ListView control in your toolbox and drag it into your page/control. Set the d"Choose Data Source" selection to your CSLADataSource object name. Click on "Configure Data Source" and set the "Business object type" to your CSLA list and that is it! At this point, if you want, you can configure your ListView to show/hide data, put themes, adjust colors, etc.

Insert, Update, and Delete
Since with CSLA list objects you cannot natively call Insert method or Update or Delete (and CSLA by default also does not have a default constructor), you have to implement the event handler for "updating", "inserting" and "deleting". So instead of using the default methods, you will actually use the standard CSLA methods for saving object (.Save()). In inserting, a factory method call New is made and then populate the object using the data mapper and Save() it. In updating, similar process, but instead of New, a call to Get is made, then populate and then Save(). Here is an example for updating (inserting and deleting should be pretty similar).

protected void ListView1_ItemInserting (object sender, ListViewInsertEventArgs e)
{
MyClass myObject = MyClass.GetMyClass((int)(e.Keys[0].ToString()));
Csla.Data.DataMapper.Map(e.NewValues, myObject);
myObject = myObject.Save();
e.Cancel = true;
ListView1.EditIndex = -1;
ReloadList();
}

Sorting
Since Sort does not work automatically like when you are binding a DataSet, so you have to do it a little manually, but it is not bad at all. First, you have to create your button for sorting, either it is a regular button or link button, etc - it does not matter. I use the column header and convert them into link buttons. Secondly, you will need to assign a CommandName and CommandArgument. Here is an example of mine:

Name

Now in the code behind, create a handler for the ItemCommand. You can actually customize your list view with all kinds of command and as along as you have a matching handler in this ItemCommand hook, then you should be good to go. There are several keywords that ListView by default has implemented: "Edit", "Insert", "Sort", "Delete". So apart from that, you can call your command anything you want.

protected void ListView1_ItemCommand (object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "CSLASort")
{
if (SortByPropertyName == e.CommandArgument.ToString())
{
if (SortDirection == System.ComponentModel.ListSortDirection.Ascending)
SortDirection = System.ComponentModel.ListSortDirection.Descending;
else
SortDirection = System.ComponentModel.ListSortDirection.Ascending;
}
else
{
SortByPropertyName = e.CommandArgument.ToString();
SortDirection = System.ComponentModel.ListSortDirection.Ascending;
}
}
}

SortDirection & SortByPropertyName are just properties of the page/control defined as such:

private string SortByPropertyName
{
get
{
if (ViewState["SortByPropertyName"] == null)
ViewState["SortByPropertyName"] = "FullName";
return (string)ViewState["SortByPropertyName"];
}
set { ViewState["SortByPropertyName"] = value; }
}

private System.ComponentModel.ListSortDirection SortDirection
{
get
{
if (ViewState["SortDirection"] == null)
ViewState["SortDirection"] = System.ComponentModel.ListSortDirection.Ascending;
return (System.ComponentModel.ListSortDirection)ViewState["SortDirection"];
}
set { ViewState["SortDirection"] = value; }
}

There you go. You can read more on ListView here.

No comments: