GridView.RowUpdated Événement
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Se produit lors d'un clic sur le bouton Mise à jour d'une ligne, mais après que le contrôle GridView a mis la ligne à jour.
public:
event System::Web::UI::WebControls::GridViewUpdatedEventHandler ^ RowUpdated;
public event System.Web.UI.WebControls.GridViewUpdatedEventHandler RowUpdated;
member this.RowUpdated : System.Web.UI.WebControls.GridViewUpdatedEventHandler
Public Custom Event RowUpdated As GridViewUpdatedEventHandler
Type d'événement
Exemples
L’exemple suivant montre comment utiliser l’événement RowUpdated pour case activée le résultat de l’opération de mise à jour. Un message s’affiche pour indiquer à l’utilisateur si l’opération a réussi.
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void CustomersGridView_RowUpdated(Object sender, GridViewUpdatedEventArgs e)
{
// Indicate whether the update operation succeeded.
if(e.Exception == null)
{
Message.Text = "Row updated successfully.";
}
else
{
e.ExceptionHandled = true;
Message.Text = "An error occurred while attempting to update the row.";
}
}
void CustomersGridView_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
{
// The update operation was canceled. Clear the message label.
Message.Text = "";
}
void CustomersGridView_RowEditing(Object sender, GridViewEditEventArgs e)
{
// The GridView control is entering edit mode. Clear the message label.
Message.Text = "";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView RowUpdated Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView RowUpdated Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<!-- The GridView control automatically sets the columns -->
<!-- specified in the datakeynames property as read-only. -->
<!-- No input controls are rendered for these columns in -->
<!-- edit mode. -->
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
autogenerateeditbutton="true"
allowpaging="true"
datakeynames="CustomerID"
onrowupdated="CustomersGridView_RowUpdated"
onrowcancelingedit="CustomersGridView_RowCancelingEdit"
onrowediting="CustomersGridView_RowEditing"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub CustomersGridView_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
' Indicate whether the update operation succeeded.
If e.Exception Is Nothing Then
Message.Text = "Row updated successfully."
Else
e.ExceptionHandled = True
Message.Text = "An error occurred while attempting to update the row."
End If
End Sub
Sub CustomersGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
' The update operation was canceled. Clear the message label.
Message.Text = ""
End Sub
Sub CustomersGridView_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
' The GridView control is entering edit mode. Clear the message label.
Message.Text = ""
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView RowUpdated Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView RowUpdated Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<!-- The GridView control automatically sets the columns -->
<!-- specified in the datakeynames property as read-only. -->
<!-- No input controls are rendered for these columns in -->
<!-- edit mode. -->
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
autogenerateeditbutton="true"
allowpaging="true"
datakeynames="CustomerID"
onrowupdated="CustomersGridView_RowUpdated"
onrowcancelingedit="CustomersGridView_RowCancelingEdit"
onrowediting="CustomersGridView_RowEditing"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
Remarques
L’événement RowUpdated est déclenché lorsque vous cliquez sur le bouton Mettre à jour d’une ligne, mais une fois que le GridView contrôle a mis à jour la ligne. Cela vous permet de fournir une méthode de gestion des événements qui effectue une routine personnalisée, telle que la vérification des résultats de l’opération de mise à jour, chaque fois que cet événement se produit.
Un GridViewUpdatedEventArgs objet est passé à la méthode de gestion des événements, ce qui vous permet de déterminer le nombre de lignes affectées et les exceptions qui ont pu se produire. Vous pouvez également indiquer si l’exception a été gérée dans la méthode de gestion des événements en définissant la ExceptionHandled propriété de l’objet GridViewUpdatedEventArgs .
Pour plus d’informations sur la façon de gérer les événements, consultez gestion et déclenchement d’événements.
S’applique à
Voir aussi
- GridViewUpdatedEventArgs
- AutoGenerateEditButton
- OnRowUpdated(GridViewUpdatedEventArgs)
- RowCancelingEdit
- OnRowCancelingEdit(GridViewCancelEditEventArgs)
- RowDeleted
- OnRowDeleted(GridViewDeletedEventArgs)
- RowDeleting
- OnRowDeleting(GridViewDeleteEventArgs)
- RowEditing
- OnRowEditing(GridViewEditEventArgs)
- RowUpdating
- OnRowUpdating(GridViewUpdateEventArgs)