Command Buttons That Trigger Events Other Than ItemCommand

Command Buttons That Trigger Events Other Than ItemCommand

As we have seen thus far, when any command button in a template is clicked, the ItemCommand event of the data Web control that contains the command button is fired. However, the DataGrid and DataList contain a bevy of other events that can be triggered in addition to the ItemCommand event. Tables 5.1 and 5.2 list the additional events for the DataGrid and DataList, respectively.

Table 5.1. Additional Command Button-Triggered Events for the DataGrid

Event

Description

CancelCommand

Occurs when a command button with a Cancel CommandName is clicked.

DeleteCommand

Occurs when a command button with a Delete CommandName is clicked.

EditCommand

Occurs when a command button with a Edit CommandName is clicked.

SortCommand

Occurs when a command button with a Sort CommandName is clicked.

UpdateCommand

Occurs when a command button with a Update CommandName is clicked.

Table 5.2. Additional Command Button-Triggered Events for the DataList

Event

Description

CancelCommand

Occurs when a command button with a Cancel CommandName is clicked.

DeleteCommand

Occurs when a command button with a Delete CommandName is clicked.

EditCommand

Occurs when a command button with a Edit CommandName is clicked.

UpdateCommand

Occurs when a command button with a Update CommandName is clicked.

The only difference between the DataGrid and DataList control is that the DataList does not contain a SortCommand event.

NOTE

The only command button event for the Repeater control is the ItemCommand event.


When a command button contains as its CommandName property one of the reserved command names Cancel, Delete, Edit, Sort, or Update when the command button is clicked, both the ItemCommand event is fired along with the appropriate additional event. That is, if you add a command button to a DataList with the CommandName Update, when the command button is clicked, the ASP.NET page will be posted back and the DataList's ItemCommand event and UpdateCommand events will be fired.

The Purpose of the Additional Events

As we've seen in this chapter, the command buttons enable developers to associate server-side event handlers with the user's click of a command button. The examples we've worked on in this chapter use these command buttons to provide a Details button that displays detailed information about a particular record in a separate Web control. Although command buttons can certainly be used for this purpose, the Microsoft developers who designed the data Web controls reasoned that one of the more common uses of these command buttons would be for sorting, deleting, and editing data.

With this assumption in mind, the developers created the additional events listed in the Tables 5.1 and 5.2 to make adding such functionality to the DataGrid and DataList much simpler. If you're wondering how these events make adding such functionality to your DataGrid or DataList simpler, imagine for a moment that those events listed in Tables 5.1 and 5.2 don't exist. If we want to add a button to the DataList that deletes the associated record, what would we need to do?

As we've seen in our examples in this chapter, we would start by creating a command button of some kind with an appropriately named CommandName (such as Delete). Next, we would probably want to use the DataKeyField/DataKeys properties to track the primary key; therefore, we would specify the DataSource's primary key field in the DataKeyField property. Following that, we are ready to add the ItemCommand event handler. In this event handler, we would check to see whether the CommandName equals Delete. If it does, we would retrieve the primary key field value of the item being deleted, and then make the appropriate database call to delete the record. Finally, we would repopulate the DataSource (because we now have one fewer record) and rebind it to the DataList.

At this point, I probably haven't convinced you that the additional events make adding such functionality easier. But imagine for a moment that we want to add an Edit command button. We haven't yet looked at how to edit data in a data Web control (we will, though, in Chapter 9, "Editing the DataGrid Web Control"), but it involves supplying an EditItemTemplate and programmatically setting the data Web control's EditItemIndex property to the row that we want to edit. Again, all this functionality can be encapsulated in the ItemCommand event handler, and the same applies to the Update and Cancel buttons that we would want to add to the EditItemTemplate.

The point is, as we add more and more command buttons, the ItemCommand event handler becomes more and more unwieldy. For example, with all the buttons listed in the preceding, the ItemCommand event handler would start to look like

 Sub dlTitles_ItemCommand(sender as Object, e as DataListCommandEventArgs)    If e.CommandName = "Delete" then      ' handle the delete case    End If    If e.CommandName = "Edit" then      ' handle the edit case    End If    If e.CommandName = "Update" then      ' handle the update case    End If    If e.CommandName = "Cancel" then      ' handle the cancel case    End If  End Sub 

and so on one If statement for each command button type. Because the code to handle each command button can be quite long, the ItemCommand event handler can very quickly balloon in size! By breaking these common command button cases into separate events, it allows for a much more readable program, which would instead look like this:

 Sub DeleteCommandEventHandler(sender as Object, e as DataListCommandEventArgs)   ' handle the delete case  End Sub  Sub EditCommandEventHandler(sender as Object, e as DataListCommandEventArgs)   ' handle the edit case  End Sub  Sub UpdateCommandEventHandler(sender as Object, e as DataListCommandEventArgs)   ' handle the update case  End Sub  Sub CancelCommandEventHandler(sender as Object, e as DataListCommandEventArgs)   ' handle the cancel case  End Sub 

We will not be looking at how to add edit, delete, and sorting capabilities to the data Web controls in this chapter. Instead there are future chapters reserved for the discussion of such functionality. These chapters fall in Part III, "Advanced Features of the DataGrid Web Control":

  • Chapter 7 "Sorting the DataGrid's Data"

  • Chapter 9 "Editing the DataGrid Web Control"

  • Chapter 10 "Putting It All Together A Real-World Example"

If you're anxious to get to this exciting material, don't worry. The next chapter is the last chapter in Part II after that, we'll dive headfirst into sorting, paging, and editing the DataGrid control!



ASP. NET Data Web Controls Kick Start
ASP.NET Data Web Controls Kick Start
ISBN: 0672325012
EAN: 2147483647
Year: 2002
Pages: 111

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net