The following example demonstrates how to use the RowUpdating event to update the values in the data source object when the data source is set programmatically. The RowUpdating event is raised when a row's Update button is clicked, but before the GridView control updates the row. This enables you to provide an event-handling method that performs a custom routine, such as canceling the update operation, whenever this event occurs.
A GridViewUpdateEventArgs object is passed to the event-handling method, which enables you to determine the index of the current row and to indicate that the update operation should be canceled. You can also manipulate the Keys , OldValues , and NewValues collections, if necessary, before the values are passed to the data source. A common way to use these collections is to HTML-encode the values supplied by the user before they are stored in the data source.
This helps to prevent script injection attacks. For more information about how to handle events, see Handling and Raising Events. We need to use two namespaces. The first one accesses the connection string and the other is for the SQL connection.
Now we define a method for opening a connection with the relation database and returns a SQL connection object depending on the connection string that is passed as a parameter of the SqlConnection class constructor. We create a method to get all employee details and bind the retrieved data with the employee grid. So first we need to use a namespace, as in: using System.
Data; Now create a method for get employee details. To populate the GridView we need to call this method on page load. Delete a Record We can delete a record from the database table by clicking on the delete button that is in the Delete column of the GridView. We first of all create a Stored Procedure that gets the Id of an employee as a parameter from the GridView on whichever row the user clicked.
In this event, first we get the datakey value that is the id of an employee of whichever row the user clicked and that id is passed as a parameter to the Stored Procedure. After that the GridView is rebound with new data. Update a Record To update a record in the GridView we need to perform the following three operations:.
According to the above, in these three operations we use three events of the GridView for each operation. Point numbers 2 and 3 are performed after point 1 is performed. Row move in edit mode We have a GridView with records. We can't update a record until that record doesn't exist in the input type field TextBox. So first we need to move to an updateable row record in the TextBox.
To do that we need a RowEditing event of the GridView. When the row is in edit mode and we don't want to update a record then we want to move to the original state of the GridView so we use the RowCancelingEdit event of the GridView. The EditIndex property of a GridView determines the index of a row in edit mode. Update record and get updated state of the GridView. To specify whether page validation is performed before the update operation, use the causesValidation parameter.
Calling this method also raises the RowUpdated and RowUpdating events. Skip to main content. This browser is no longer supported.
Download Microsoft Edge More info. Contents Exit focus mode. Grid View. Please rate your experience Yes No.
0コメント