How to get Index of row with button_click event

  Uncategorised

<asp:GridView class="table table-striped table-bordered " ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="dish_id" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand">

<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. I watched some tutorials that have used checkbox change events, but I want to get id using button_click Event. Is it possible?

Source: Visual Studio Questions

LEAVE A COMMENT