asp.net.ph

DataGrid.UpdateCommand Event

System.Web.UI.WebControls Namespace   DataGrid Class


Occurs when an update command button associated with an item in the DataGrid control is clicked.

[ VB ]
Public Event UpdateCommand As DataGridCommandEventHandler

[ C# ]
public event DataGridCommandEventHandler UpdateCommand;

[ C++ ]
public: __event DataGridCommandEventHandler* UpdateCommand;

In [ JScript ], you can handle the events defined by a class, but you cannot define your own.

Remarks

The UpdateCommand event is raised whenever an Update button associated with an item in the DataGrid control is clicked. The event is typically used to trigger a handler that updates any edits made to an item in the DataGrid control.

Controls such as update command buttons that are defined within the items of a parent container such as the DataGrid bubbles their event up the control hierarchy to the containing DataGrid control. This provides a convenient way to assign one handler for the same command event at the DataGrid level, as only one update command can be activated at any time for any item in the grid. You do not have to assign the same handler for each update button.

Event Data

Information related to the UpdateCommand event is passed via a DataGridCommandEventArgs object to the method assigned to handle the event. The following DataGridCommandEventArgs properties provide information specific to this event.

Property Description
CommandArgument ( inherited from CommandEventArgs ) Gets the argument for the command.
CommandName ( inherited from CommandEventArgs ) Gets the name of the command.
CommandSource Gets the source of the command.
Item Gets the selected item in the DataGrid.

Example

The following code snippets illustrate how to specify and code a handler for the UpdateCommand event, to update the data source with any edits made to an item in a DataGrid control.

The below snippet shows how to attach a handler for the event.

<asp:datagrid id = "myGrid" runat = "server" 
   onUpdateCommand = "myGridUpdateHandler" ... >

The below shows how the handler method is defined, which basically updates the corresponding row in the data source, then resets the EditItemIndex property to -1, essentially setting edit mode off. The grid is then re-bound to the source to refresh the display back to the default grid layout.

void myGridUpdateHandler ( Object src, DataGridCommandEventArgs e ) {
   // e.Item represents the current row in edit mode ...
   // for bound columns, the edited value is stored in a textbox,
   // and the textbox is the 0th element in the column's cell
   TextBox titleText = ( TextBox ) e.Item.Cells [ 1 ].Controls [ 0 ];
   TextBox priceText = ( TextBox ) e.Item.Cells [ 2 ].Controls [ 0 ];

   // get edited row values in grid
   string title = titleText.Text;
   float price = float.Parse ( priceText.Text );

   // load data from Session
   DataTable myTable = ( DataTable ) Session [ "myTable" ];

   // for demo purposes, the following procedure updates the row
   // only in the datatable bound to the grid, not in the database ...
   // in actual practice, though, you can instead
   // call a method to update the row in the database here ... 

   // get and update row
   DataRow myRow = myTable.Rows [ e.Item.ItemIndex ];
   myRow [ "title" ] = title;
   myRow [ "price" ] = price;
   myTable.AcceptChanges ( );

   // refresh data in Session
   Session [ "myTable" ] = myTable;

   // set edit mode off
   myGrid.EditItemIndex = -1;
   bindGrid ( );
}

 Show me 

See Also

DataGrid Members   CancelCommand   DeleteCommand   EditCommand   Allowing Users to Edit Items in a DataGrid Control Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph