简体   繁体   中英

Get selected Index in gridview by clicking on button

I have grid view and there is a template field in it and i want to get the selected row's index by clicking on button i cant change the field type cause i want to change the visibale attribute in my behind code manually. Here is my code:

    <asp:GridView ID="gvService" runat="server" BackColor="White" AutoGenerateColumns="False"
    BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" DataKeyNames="Service_Id, UserId">
    <AlternatingRowStyle BackColor="#DCDCDC" />
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" HeaderStyle-BackColor="Gray">
            <HeaderStyle BackColor="Gray"></HeaderStyle>
        </asp:BoundField>
        <asp:BoundField DataField="L_Name" HeaderText="LastName" SortExpression="L_Name"
            HeaderStyle-BackColor="Gray">
            <HeaderStyle BackColor="Gray"></HeaderStyle>
          <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="bttCost" runat="server" Text="Cost" OnCommand="Cmd_Pay" Visible='<%# IsNotPaid((Decimal)Eval("Cost")) %>' />
            </ItemTemplate>
          </asp:TemplateField>

and my behind code:

        int rowIndex = gvService.SelectedIndex;
        HFServiceID.Value = (string)gvService.DataKeys[rowIndex]["Service_Id"];
        HFUserID.Value  = (string)gvService.DataKeys[rowIndex]["UserId"];

要获取被单击按钮所属的行索引,请将其添加到onclick事件中。

var rowIndex = ((GridViewRow)((Control)sender).NamingContainer).RowIndex;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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