2011-12-15 94 views
0

获取选定的索引在gridview中我有网格视图,并有一个模板字段,我想通过单击按钮来获取选定的行索引我不能改变字段类型因为我想改变visibale属性在我的后面的代码手动。 这里是我的代码:通过单击按钮

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

和我的身后代码:

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

我看不到按钮或模板字段 – 2011-12-15 20:56:21

回答

3

要获得被点击的按钮所属的,在你的onclick事件添加此行索引。

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