web-dev-qa-db-fra.com

ASP.NET GridView RowIndex As CommandArgument

Comment accéder et afficher l'index de ligne d'un élément gridview en tant qu'argument de commande dans un bouton de colonne buttonfield?

<gridview>
<Columns>
   <asp:ButtonField  ButtonType="Button" 
        CommandName="Edit" Text="Edit" Visible="True" 
        CommandArgument=" ? ? ? " />
.....
55
mirezus

Voici un moyen très simple:

<asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True" 
                 CommandArgument='<%# Container.DataItemIndex %>' />
95
George

MSDN dit que:

La classe ButtonField remplit automatiquement la propriété CommandArgument avec la valeur d'index appropriée. Pour les autres boutons de commande, vous devez définir manuellement la propriété CommandArgument du bouton de commande. Par exemple, vous pouvez définir le CommandArgument sur <% # Container.DataItemIndex%> lorsque la pagination du contrôle GridView n'est pas activée.

Donc, vous ne devriez pas avoir besoin de le configurer manuellement. Une commande de ligne avec GridViewCommandEventArgs la rendrait alors accessible; par exemple.

protected void Whatever_RowCommand( object sender, GridViewCommandEventArgs e )
{
    int rowIndex = Convert.ToInt32( e.CommandArgument );
    ...
}
35
Rich

Voici une suggestion de Microsoft pour cela http://msdn.Microsoft.com/en-us/library/bb907626.aspx#Y800

Dans la vue en grille, ajoutez un bouton de commande et convertissez-le en modèle, puis nommez-le dans cet exemple "AddToCart" et ajoutez également CommandArgument "<% # ((GridViewRow) Container)) .RowIndex %> "

<asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="AddButton" runat="server" 
      CommandName="AddToCart" 
      CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
      Text="Add to Cart" />
  </ItemTemplate> 
</asp:TemplateField>

Puis pour create sur l'événement RowCommand de gridview, identifiez le moment où la commande "AddToCart" est déclenchée et faites ce que vous voulez à partir de là.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "AddToCart")
  {
    // Retrieve the row index stored in the 
    // CommandArgument property.
    int index = Convert.ToInt32(e.CommandArgument);

    // Retrieve the row that contains the button 
    // from the Rows collection.
    GridViewRow row = GridView1.Rows[index];

    // Add code here to add the item to the shopping cart.
  }
}

** Une erreur que je faisais est que je voulais ajouter les actions sur mon bouton de modèle au lieu de le faire directement sur l'événement RowCommand.

12
Cesar Duran

Je pense que ça va marcher.

<gridview>
<Columns>

            <asp:ButtonField  ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True" CommandArgument="<%# Container.DataItemIndex %>" />
        </Columns>
</gridview>
5
Christopher Edwards
void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    Button b = (Button)e.CommandSource;
    b.CommandArgument = ((GridViewRow)sender).RowIndex.ToString();
}
1
fARcRY

Je lie généralement ces données en utilisant l'événement RowDatabound avec GridView:

protected void FormatGridView(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow) 
   {
      ((Button)e.Row.Cells(0).FindControl("btnSpecial")).CommandArgument = e.Row.RowIndex.ToString();
   }
}
0
Dillie-O
<asp:TemplateField HeaderText="" ItemStyle-Width="20%" HeaderStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:LinkButton runat="server" ID="lnkAdd" Text="Add" CommandName="Add" CommandArgument='<%# Eval("EmpID"))%>' />
                    </ItemTemplate>
                </asp:TemplateField>

C'est la manière traditionnelle et la dernière version du framework asp.net d'avoir des données fortement typées et vous n'avez pas besoin d'utiliser une chaîne telle que "EMPID"

0
Bala