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

Session 24—Beating the CRUD out of the DataGrid Control

247

Next you must rebind the DataSet by calling the DataBind function. This will render the OK and Cancel hyperlinks that we established when building the EditCommandColumn. After the user has had the opportunity to edit the selected field, he or she can then select to process the update by selecting the OK hyperlink or to cancel the update with the Cancel hyperlink.

Handling the OnCancelCommand Event

Should the user choose to cancel the update then the OnCancelCommand event fires, which we have associated with the OnCanel subroutine. This routine simply sets the DataGrid control back to ReadOnly by setting the EditItemIndex to –1, then we simply rebind the data to the grid with the BindData function, as shown in the following example:

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

sender.EditItemIndex=-1 BindData()

Message.Text = “Status: Update Canceled” Catch myException as Exception

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

End Sub

Handling the OnUpdateCommand Event

When the user selects the OK hyperlink the OnUpdateCommand event fires. We have attached this event to the OnUpdate subroutine. In order to update the data, you need to have a way of effectively getting the user’s updated values.

When we originally set up the TemplateColumn responsible for handling the display of the data, we set up an ItemTemplate that handles the formatting and display of values when the grid is in ReadOnly mode. At the same time, we set the EditItemTemplate to handle the display of values when the grid is in Edit mode.

<ASP:TEMPLATECOLUMN HEADERTEXT=”Unit Price” SORTEXPRESSION=”Price”> <ITEMTEMPLATE>

<ASP:LABEL id=”Label2” RUNAT=”server” TEXT=’<%# String.Format(“{0:C}”, Container.DataItem(“price”))%>’ WIDTH=”50” AUTOSIZE=”True”></ASP:LABEL>

</ITEMTEMPLATE>

<EDITITEMTEMPLATE>

<ASP:TEXTBOX id=”editprice” RUNAT=”Server” TEXT=’<%# Container.DataItem(“price”)%>’ 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>

248

Sunday Morning

In the EditItemTemplate we set up a textbox to handle the display of editable data and gave it an id of editprice. Additionally we set up a validation control with an id of valeditprice to make sure that the user enters a positive currency value. Below we will cover how we use the validation control to validate the users input.

Checking that the user input has been validated

When the OnUpdate subroutine fires, the first thing to do is insure that the validator control, valeditprice is valid. If it is valid, you can then proceed with the update. We use the following code shown in Listing 24-2 to find the validation control and check that the values are within acceptable parameters. Assuming that the control is valid, you can then begin the update process.

Executing the update process

In our OnUpdate routine shown in Listing 24-2 we use the following line of code to find the editprice text box: txtBox = e.item.findcontrol(“editprice”).

Listing 24-2 OnUpdate subroutine

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

Dim sTitleId as String Dim dPrice as decimal Dim txtBox as TextBox

Dim valCtrl as CompareValidator

valCtrl = e.item.findcontrol(“valeditprice”) If valCtrl.isValid

sTitleId = titlegrid.datakeys.item(e.item.itemindex) txtBox = e.item.findcontrol(“editprice”)

dPrice =txtBox.Text UpdateTitles(dPrice, sTitleId) titleGrid.EditItemIndex=-1 BindData()

Message.Text =”Status: Update Completed” Else

Message.Text =”Status: No Update, Validation Failed” End If

Catch myException as Exception

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

End Sub

We then retrieve its contents with the following code:

dPrice =txtBox.Text