Handling the OnUpdate Command Event

Crypto Ultimatum

How to Make Money Investing in Bitcoin, Cryptocurrency

Get Instant Access

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: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>


In the EditltemTemplate 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 valed-itprice 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 sTitleld as String

Dim dPrice as decimal

Dim txtBox as TextBox

Dim valCtrl as CompareValidator valCtrl = e.item.findcontrol("valeditprice")

If valCtrl.isValid sTitleld = titlegrid.datakeys.item(e.item.itemindex)

txtBox = e.item.findcontrol("editprice")

dPrice =txtBox.Text

UpdateTitles(dPrice, sTitleld)



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

The last piece of information we need to call our UpdateTitles function is the Title Id, which can be found by looking at the bound datakeys collection and passing the edited rows itemindex value:

sTitleld = titlegrid.datakeys.item(e.item.itemindex)

Now that we have the Title Id and the updated price, we can call our UpdateTitles function and pass these values. Finally, before we finish we will need to set the DataGrid control back to ReadOnly and rebind the updated data to the grid so that the cell is set to a read-only status.



The following code provides a listing of the UpdateTitles subroutine, which actually performs the database update of the price:

Sub UpdateTitles(price As Decimal, title_id As String)

Dim connection as New OleDBConnection("provider=sqloledb;Data

Source=localhost;Initial Catalog=pubs;User ID=sa;pwd=")

Dim command as New OleDbCommand("UPDATE titles SET [price]=? WHERE [title_id] =?", connection)

Dim paramO as New OleDBParameter("price", OleDBType.Currency)

paramO.Value = price command.Parameters.Add(paramO)

Dim paraml as New OleDBParameter("title_id", OleDBType.VarChar)

paraml.Value = title_id command.Parameters.Add(paraml) connection.Open() command.ExecuteNonQuery() connection.Close()

Unfortunately, the EditCommandColumn doesn't provide an automated way to generate a 10 Min. Delete or Select hyperlink. However, you can use the ButtonColumn to implement a Delete To Go method for the DataGrid control as shown in the following code:

<asp:ButtonColumn Text="Delete" CommandName="Delete"> </asp:ButtonColumn>

We attach a subroutine to execute when the hyperlink is selected as illustrated in Figure 24-2 with the following attribute of the DataGrid control:


The following code illustrates the subroutine that fires when the hyperlink is selected:

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

Dim sTitleld as String

End Sub

Was this article helpful?

0 0

Post a comment