简体   繁体   English

如何使用 button_click 事件获取行索引

[英]How to get Index of row with button_click event

<asp:GridView class="table table-striped table-bordered " ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="dish_id" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:BoundField DataField="dish_id" HeaderText="ID" ReadOnly="True" SortExpression="dish_id" />

        <asp:TemplateField>
            <ItemTemplate>
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-8">
                            <div class="row">
                                <div class="col-12">
                                    <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="X-Large" Text='<%# Eval("dish_name") %>'></asp:Label>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-12">
                                    Category-<asp:Label ID="Label2" Font-Bold="True" runat="server" Text='<%# Eval("cat_name") %>'></asp:Label>
                                    &nbsp;| Cuisine-
                                                    <asp:Label ID="Label3" Font-Bold="True" runat="server" Text='<%# Eval("cuisine") %>'></asp:Label>
                                    &nbsp;| Price-
                                                    <asp:Label ID="Label4" Font-Bold="True" runat="server" Text='<%# Eval("price") %>'></asp:Label>

                                </div>
                            </div>

                            <div class="row">
                                <div class="col-12">
                                    Description-
                                                    <asp:Label Font-Bold="True" ID="Label5" runat="server" Text='<%# Eval("dish_description") %>'></asp:Label>

                                </div>
                            </div>
                        </div>
                        <div class="col-lg-2">
                            <asp:Image class="img-fluid" ID="Image1" runat="server" ImageUrl='<%# Eval("dish_img_link") %>' />
                        </div>
                        <div class="col-lg-2">
                            <asp:Button ID="Button1" class="btn btn-outline-info btn-block btn-sm" runat="server" Text="Add to Cart" OnClick="Button1_Click" />
                        </div>
                    </div>
                </div>
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>

I have this GridView & button (Add to Cart) in front of each row, I want to the get ID of the specific row whose button is clicked.我在每一行前面都有这个 GridView & 按钮(添加到购物车),我想获取单击其按钮的特定行的 ID。 I watched some tutorials that have used checkbox change events, but I want to get id using button_click Event.我观看了一些使用复选框更改事件的教程,但我想使用 button_click 事件获取 id。 Is it possible?可能吗?

You don't mention if this is a client side Java click, or a server side one?您没有提到这是客户端 Java 点击还是服务器端? And when you want to START using custom controls and standard asp.net controls in a grid?当您想开始在网格中使用自定义控件和标准 asp.net 控件时? Then I would STRONG recommend you flip over to use a list-view.那么我强烈建议您翻转以使用列表视图。 They are much similar, but list-views support standard controls WITHOUT having to wrap the control in a template - thus markup is MUCH cleaner, and you can with greater ease drop in buttons etc. into that given row.它们非常相似,但列表视图支持标准控件,而不必将控件包装在模板中 - 因此标记更加清晰,您可以更轻松地将按钮等放入给定的行中。

The cheating way (by-pass all gridview events), power past the issue, don't care about much?作弊方式(绕过所有gridview事件),电源过去的问题,不要太在意?

Then do this for your button:然后为您的按钮执行此操作:

<asp:Button ID="Button1" class="btn btn-outline-info btn-block btn-sm" runat="server"
 Text="Add to Cart" OnClick="Button1_Click" 
 MyPKID = '<%# Eval("ID") %>'
 MyRow  = '<%# Container.DataItemIndex %>'
 />

Note VERY interesting how I grab both the pK of the data row (in above example the primary key row column name was "ID").请注意,我如何同时获取数据行的 pK 非常有趣(在上面的示例中,主键行列名称是“ID”)。 This is a GREAT trick since OFTEN you do NOT want the PK row id to display in the grid, and it can be a PAIN to hide it.这是一个很棒的技巧,因为您通常不希望 PK 行 ID 显示在网格中,隐藏它可能会很痛苦。 So in above, I grab BOTH PK of the data base row, and also the row index - was not really sure which one you wanted - so lets get BOTH values.因此,在上面,我获取了数据库行的 BOTH PK 以及行索引 - 不确定你想要哪一个 - 所以让我们获取 BOTH 值。

Ok, now in your click event, you can do this:好的,现在在您的点击事件中,您可以这样做:

protected void btnRowJump_Click(object sender, EventArgs e)

{
Button btn = sender;

int MyRow = btn.Attributes.Item("MyRow");
int MyPKID = btn.Attributes.Item("MyPKID");

Debug.Print("Database PK id = " + MyPKID.ToString());
Debug.Print("grid row click =  = " + MyRow.ToString());

// Get grid row data item

GridViewRow MyGvRow = GridView1.Rows(MyRow);
}

So using your own "custom" attriributes can often be handy.因此,使用您自己的“自定义”属性通常会很方便。 I might have SEVERAL data items in that database row - but NOT displayed in the grid.我可能在该数据库行中有几个数据项 - 但未显示在网格中。 So you see I do this:所以你看我这样做:

 <asp:LinkButton ID="pUploadFiles"  runat="server" 
                    Text='Upload Files'

         PortalComp      = '<%# Eval("PortalComp")%>'
         ContactNameID   = '<%# Eval("ContactNameID")%>'
         QuoteNum        = '<%# Eval("QuoteNum")%>'
         ProjectHeaderID = '<%# Eval("ID")%>'
         ContactGeneralID = '<%# Eval("ContactGeneralID")%>'
   </asp:LinkButton>

So as LONG as the data rows exist at data bind time, then I don't have to include these rows in the display part - but any row from the data source at bind time is available with above.因此,只要数据行在数据绑定时存在,那么我不必在显示部分中包含这些行 - 但在绑定时来自数据源的任何行都可以在上面使用。 So in above, those rows were NOT displayed in the grid, but I still get/grab them into a custom attributes and pluck them out as per the code example.所以在上面,这些行没有显示在网格中,但我仍然将它们获取/抓取到自定义属性中,并按照代码示例将它们取出。

Having stated the above?说了上面的话? Do keep in mind that the above trick does NOT trigger the GridView change index event - so you can't say in code go请记住,上述技巧不会触发 GridView 更改索引事件 - 所以你不能在代码中说 go

        MyRvRow2 = GridView1.SelectedRow

However, I find often I don't care or need or want the actual grid row selected to change, and it not a big deal.但是,我经常发现我并不关心或不需要或希望更改选择的实际网格行,这没什么大不了的。 And you can force the row to change with this:您可以通过以下方式强制更改行:

    GridView1.SelectedIndex = MyRow

Most examples suggest to set CommandName = "My Select" and then use CommandArugment.大多数示例建议设置 CommandName = "My Select",然后使用 CommandArugment。 This means you don't have a separate button click event stub, but use the RowCommand event of the gridview.这意味着您没有单独的按钮单击事件存根,而是使用 gridview 的 RowCommand 事件。 With the above "custom attribote" trick, you tend to wind up writing a wee bit less code, but you get nice separate code event stubs for each button - and I in a lot of cases prefer this over that of leveraging and using the GridView event model.使用上面的“自定义属性”技巧,您最终会编写更少的代码,但您会为每个按钮获得很好的单独代码事件存根 - 在很多情况下,我更喜欢这个而不是利用和使用 GridView事件 model。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM