如何知道在asp.net gridView中单击按钮的哪一行

时间:2015-11-07 11:41:00

标签: c# jquery asp.net gridview

这是我在aspx网页中使用的简单gridView

     <asp:GridView ID="Order" runat="server" AutoGenerateColumns="False" ShowFooter="True" GridLines="none"
    ItemType="MyProject.Models.TempList" SelectMethod="GetList" >   
    <Columns>
        <asp:BoundField DataField="ItemID" HeaderText="ID" SortExpression="ItemID" />

        <asp:TemplateField   HeaderText="ItemName">  
            <ItemTemplate>
                <asp:Label runat="server" ID="ItemName" Width="40" Visible="true" text="<%#: Item.Item.ItemName %>"></asp:Label>  
            </ItemTemplate>                      
        </asp:TemplateField>

        <asp:TemplateField   HeaderText="Price(each)">  
            <ItemTemplate>
                <%#: String.Format("{0:c}", Convert.ToDouble(Item.Item.UnitPrice))%>                    
            </ItemTemplate>                         
        </asp:TemplateField>                                                                 

        <asp:TemplateField HeaderText="Delete">            
            <ItemTemplate>
                <div style="float:left">               
             <asp:Button ID="DeleteBtn" runat="server" Text="Delete" OnClick="DeleteBtn_Click"/>                           
            </ItemTemplate>        
        </asp:TemplateField>              
    </Columns>  

</asp:GridView>

我在列表中有2个以上的项目&lt;&gt;这是填充gridView,我怎么能告诉(在codeBehind.cs中)哪一行点击了“DeleteBtn”?

我使用for循环使用Rows [i]迭代gridView中的每个项目,并使用复选框知道要使用“更新”按钮删除哪个项目。

但我想直接在自定义创建的删除按钮上执行此操作。

在此先感谢,我知道我错过了一些愚蠢的事情。

2 个答案:

答案 0 :(得分:1)

最好的方法是在你的按钮声明中使用CommandName和CommandArgument,就像那样

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

你可以在我们传递rowIndex的情况下传递任何值,你可以从你的对象获得一个属性,如CommandArgument =“&lt;%#Eval(”id“)%&gt;”之后,您将处理onRowCommand方法

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.
  }

  }

希望它有所帮助:)

答案 1 :(得分:1)

使用按钮的CommandArgument属性;为其分配Container.DataItemIndex。然后使用gridview的OnRowCommand事件并获取索引。

示例aspx:

<asp:Label runat="server" ID="lblMsg" />
<asp:GridView runat="server" id="gvSample" AutoGenerateColumns="false" OnRowCommand="PerformOperation">
    <Columns>
        <asp:BoundField DataField="RowValue"/>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button Text="Delete" runat="server" CommandName="MyCustomCommand" CommandArgument="<%# Container.DataItemIndex %>" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码背后:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        PopulateGrid();
    }
}

private void PopulateGrid()
{
    gvSample.DataSource = Enumerable.Range(0, 10).Select(i => new { RowValue = i });
    gvSample.DataBind();
}

protected void PerformOperation(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "MyCustomCommand")
    {
            var rowIndex = int.Parse(e.CommandArgument);
            lblMsg.Text = string.Format("Button on row index: {0} was clicked!", rowIndex);
    }
}