Cliquez pour évaluer et commenter
MSDN
MSDN Library
Développement .NET
.NET Framework 3.5
.NET Framework
Bibliothèque de classes ....
GridView, classe
Événements GridView
 RowCreated, événement

  Passer à l'affichage pour faible bande passante
Cette page est spécifique à
Microsoft Visual Studio 2008/.NET Framework 3.5

D'autres versions sont également disponibles pour :
Bibliothèque de classes .NET Framework
GridView..::.RowCreated, événement

Mise à jour : novembre 2007

Se produit lorsqu'une ligne est créée dans un contrôle GridView.

Espace de noms :  System.Web.UI.WebControls
Assembly :  System.Web (dans System.Web.dll)

Visual Basic (Déclaration)
Public Event RowCreated As GridViewRowEventHandler
Visual Basic (Utilisation)
Dim instance As GridView
Dim handler As GridViewRowEventHandler

AddHandler instance.RowCreated, handler
C#
public event GridViewRowEventHandler RowCreated
VisualC++
public:
 event GridViewRowEventHandler^ RowCreated {
    void add (GridViewRowEventHandler^ value);
    void remove (GridViewRowEventHandler^ value);
}
J#
/** @event */
public void add_RowCreated (GridViewRowEventHandler value)
/** @event */
public void remove_RowCreated (GridViewRowEventHandler value)
JScript
JScript ne prend pas en charge les événements.
ASP.NET
<asp:GridView OnRowCreated="GridViewRowEventHandler" />

Avant que le contrôle GridView ne puisse être rendu, un objet GridViewRow doit être créé pour chaque ligne du contrôle. L'événement RowCreated est déclenché lorsqu'un élément figurant dans le contrôle GridView est créé. Cela vous permet de fournir une méthode de gestion d'événements qui exécute une routine personnalisée, par exemple l'ajout d'un contenu personnalisé à une ligne, chaque fois que cet événement se produit.

Un objet GridViewRowEventArgs est passé à la méthode de gestion d'événements qui vous permet d'accéder aux propriétés de la ligne en cours de création. Pour accéder à une cellule spécifique de la ligne, utilisez la propriété Cells de l'objet GridViewRowEventArgs. Vous pouvez déterminer le type de ligne (ligne d'en-tête, ligne de données, etc.) créé en utilisant la propriété RowType.

Pour plus d'informations sur la gestion d'événements, consultez Consommation d'événements.

L'exemple suivant montre comment utiliser l'événement RowCreated pour stocker l'index de la ligne en cours de création dans la propriété CommandArgument d'un contrôle LinkButton contenu dans la ligne. Cela vous permet de déterminer l'index de la ligne qui contient le contrôle LinkButton lorsque l'utilisateur clique sur le bouton.

Visual Basic
<%@ 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_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

    ' If multiple buttons are used in a GridView control, use the
    ' CommandName property to determine which button was clicked.
    If e.CommandName = "Add" Then

      ' Convert the row index stored in the CommandArgument
      ' property to an Integer.
      Dim index As Integer = Convert.ToInt32(e.CommandArgument)

      ' Retrieve the row that contains the button clicked 
      ' by the user from the Rows collection.
      Dim row As GridViewRow = CustomersGridView.Rows(index)

      ' Create a new ListItem object for the customer in the row.     
      Dim item As New ListItem()
      item.Text = Server.HtmlDecode(row.Cells(2).Text)

      ' If the customer is not already in the ListBox, add the ListItem 
      ' object to the Items collection of the ListBox control. 
      If Not CustomersListBox.Items.Contains(item) Then

        CustomersListBox.Items.Add(item)

      End If

    End If

  End Sub

  Sub CustomersGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    ' The GridViewCommandEventArgs class does not contain a 
    ' property that indicates which row's command button was
    ' clicked. To identify which row's button was clicked, use 
    ' the button's CommandArgument property by setting it to the 
    ' row's index.
    If e.Row.RowType = DataControlRowType.DataRow Then

      ' Retrieve the LinkButton control from the first column.
      Dim addButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)

      ' Set the LinkButton's CommandArgument property with the
      ' row's index.
      addButton.CommandArgument = e.Row.RowIndex.ToString()

    End If

  End Sub

</script>

<html  >
  <head runat="server">
    <title>GridView RowCommand Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView RowCommand Example</h3>

      <table width="100%">         
        <tr>                
          <td style="width:50%">

            <asp:gridview id="CustomersGridView" 
              datasourceid="CustomersSource"
              allowpaging="true" 
              autogeneratecolumns="false"
              onrowcommand="CustomersGridView_RowCommand"
              onrowcreated="CustomersGridView_RowCreated"  
              runat="server">

              <columns>
                <asp:buttonfield buttontype="Link" 
                  commandname="Add" 
                  text="Add"/>
                <asp:boundfield datafield="CustomerID" 
                  headertext="Customer ID"/>
                <asp:boundfield datafield="CompanyName" 
                  headertext="Company Name"/> 
                <asp:boundfield datafield="City" 
                  headertext="City"/>         
              </columns>

            </asp:gridview>

          </td>

          <td style="vertical-align:top; width:50%">

            Customers: <br/>
            <asp:listbox id="CustomersListBox"
              runat="server"/> 

          </td>  
        </tr>      
      </table>

      <!-- 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="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>

    </form>
  </body>
</html>


C#
<%@ 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_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Add")
    {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);

      // Retrieve the row that contains the button clicked 
      // by the user from the Rows collection.
      GridViewRow row = CustomersGridView.Rows[index];

      // Create a new ListItem object for the customer in the row.     
      ListItem item = new ListItem();
      item.Text = Server.HtmlDecode(row.Cells[2].Text);

      // If the customer is not already in the ListBox, add the ListItem 
      // object to the Items collection of the ListBox control. 
      if (!CustomersListBox.Items.Contains(item))
      {
        CustomersListBox.Items.Add(item);
      }           
    }
  }

  void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
  {

    // The GridViewCommandEventArgs class does not contain a 
    // property that indicates which row's command button was
    // clicked. To identify which row's button was clicked, use 
    // the button's CommandArgument property by setting it to the 
    // row's index.
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Retrieve the LinkButton control from the first column.
      LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];

      // Set the LinkButton's CommandArgument property with the
      // row's index.
      addButton.CommandArgument = e.Row.RowIndex.ToString();
    }

  }

</script>

<html  >
  <head runat="server">
    <title>GridView RowCommand Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView RowCommand Example</h3>

      <table width="100%">         
        <tr>                
          <td style="width:50%">

            <asp:gridview id="CustomersGridView" 
              datasourceid="CustomersSource"
              allowpaging="true" 
              autogeneratecolumns="false"
              onrowcommand="CustomersGridView_RowCommand"
              onrowcreated="CustomersGridView_RowCreated"  
              runat="server">

              <columns>
                <asp:buttonfield buttontype="Link" 
                  commandname="Add" 
                  text="Add"/>
                <asp:boundfield datafield="CustomerID" 
                  headertext="Customer ID"/>
                <asp:boundfield datafield="CompanyName" 
                  headertext="Company Name"/> 
                <asp:boundfield datafield="City" 
                  headertext="City"/>         
              </columns>

            </asp:gridview>

          </td>

          <td style="vertical-align:top; width:50%">

            Customers: <br/>
            <asp:listbox id="CustomersListBox"
              runat="server"/> 

          </td>  
        </tr>      
      </table>

      <!-- 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="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>

    </form>
  </body>
</html>


Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professionnel Édition x64, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

Le .NET Framework et le .NET Compact Framework ne prennent pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.

.NET Framework

Pris en charge dans : 3.5, 3.0, 2.0
Contenu de la communauté   Qu'est-ce que le Contenu de la communauté ?
Ajouter du contenu RSS  Annotations
Processing
© 2009 Microsoft Corporation. Tous droits réservés. Conditions d'utilisation  |  Marques  |  Confidentialité
Page view tracker