【问题标题】:How to know in which row a button is clicked in asp.net gridView如何知道在asp.net gridView中单击了哪一行按钮
【发布时间】:2015-11-07 11:41:00
【问题描述】:

这是我在 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 个以上的项目 正在填充 gridView,我如何知道(在 codeBehind.cs 中)点击了“DeleteBtn”的哪一行?

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

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

提前谢谢,我知道我错过了一些愚蠢的东西。

【问题讨论】:

  • 使用CommandNameCommandArgumentOnRowCommandHere are some hints (see the example).
  • @mshsayem 感谢您的回复。您能否发布作为答案并快速预览在何处以及如何使用它们?或将我链接到解释它的页面。

标签: c# jquery asp.net gridview


【解决方案1】:

最好的方法是在按钮声明中使用 CommandName 和 CommandArgument

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

在我们传递 rowIndex 的情况下,您可以传递任何值,您可以从对象中获取属性,例如 CommandArgument="" 之后您将处理 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.
  }

  }

希望对你有帮助:)

【讨论】:

    【解决方案2】:

    使用按钮的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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-04
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      相关资源
      最近更新 更多