2011-12-15 140 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;