Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET Database Programming Weekend Crash Course - J. Butler, T. Caudill.pdf
Скачиваний:
31
Добавлен:
24.05.2014
Размер:
3.32 Mб
Скачать

246

Sunday Morning

Listing 24-1

Continued

WIDTH=”50”>

</ASP:TEXTBOX>

<BR>

<ASP:COMPAREVALIDATOR id=”valeditprice” RUNAT=”server” CONTROLTOVALIDATE=”editprice” ERRORMESSAGE=”You must supply a positive currency value.” TYPE=”Currency” OPERATOR=”GreaterThan” VALUETOCOMPARE=”0” DISPLAY=”dynamic”></ASP:COMPAREVALIDATOR>

</EDITITEMTEMPLATE>

</ASP:TEMPLATECOLUMN>

<ASP:BOUNDCOLUMN HEADERTEXT=”YTD Sales” DATAFIELD=”ytd_sales” SORTEXPRESSION=”ytd_sales” DATAFORMATSTRING=”{0:C}”>

</ASP:BOUNDCOLUMN> <ASP:EDITCOMMANDCOLUMN EDITTEXT=”Edit”

CANCELTEXT=”Cancel”

UPDATETEXT=”OK”>

</ASP:EDITCOMMANDCOLUMN> <ASP:BUTTONCOLUMN TEXT=”Delete”

COMMANDNAME=”Delete”>

</ASP:BUTTONCOLUMN>

</COLUMNS>

</ASP:DATAGRID>

In the following sections we will cover how to handle attaching functions to support the edit, cancel, update and delete events.

Handling the OnEditCommand Event

As illustrated in Listing 24-1, when a user selects the Edit hyperlink, the OnEditCommand event is fired, this event has been set to call the OnEdit subroutine. The following example displays the code contained in the OnEdit Sub:

Sub OnEdit(sender As Object, E As DataGridCommandEventArgs) Try

‘Set the Grid to Editable sender.EditItemIndex = E.Item.ItemIndex BindData()

Message.Text = “Status: In Edit Mode” Catch myException as Exception

Message.Text = (“Exception: “ + myException.ToString()) End Try

End Sub

This subroutine simply turns the editing mode of the DataGrid control on by setting its EditItemIndex to the value of the row the user was on when the Edit hyperlink was selected. This forces the DataGrid control to turn all fields in the selected row which are not set as ReadOnly to textboxes. When you wish to prevent a column from being edited, simply set the ReadOnly property of the column to True. In our example, the only column we are permitting the user to edit is the Unit Price column.