2013-06-19 20 views
3

我有像下面的代碼。我的問題是我無法對頁腳進行索引。使用代碼我得到了頁腳的rowindex = -1。我試圖找到網絡中的溶劑,但我沒有找到任何東西。感謝您是否會幫助我。先謝謝你。獲取頁腳的RowIndex

protected void Staff_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
    try 
    { 
     //Checking for command name which command/button is pressed 
     if (e.CommandName == "ShowDetails") 
     { 
      GridView Staff = (GridView)sender; 

      int index = Convert.ToInt32(e.CommandArgument); 
      GridViewRow row = Staff.Rows[index]; 
      GridView StaffInfo = (GridView)Staff.Rows[index].FindControl("StaffInfo"); 
      int TeamID = Convert.ToInt16(Staff.DataKeys[index].Values[0].ToString()); 
      int cityID = Convert.ToInt16(Staff.DataKeys[index].Values[1].ToString()); 
      StaffInfo.DataSource = GetStaff(cityID, TeamID); 
      StaffInfo.DataBind(); 
      StaffInfo.Visible = true; 
     } 
     else if(e.CommandName == "ShowAll") 
     { 
      int index = Convert.ToInt32(e.CommandArgument); 
      GridView StaffInfo = (GridView)Staff.Rows[index].FindControl("StaffInfo"); 
      int TeamID = 0; 
      int cityID = 3699; 
      StaffInfo.DataSource = GetStaff(cityID, TeamID); 
      StaffInfo.DataBind(); 
      StaffInfo.Visible = true; 
     } 
    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message); 
    } 
} 

<asp:GridView ID="Staff" runat="server" AutoGenerateColumns="false" 
    OnRowCommand="Staff_RowCommand" 
    DataKeyNames="TeamID,CityID" ShowFooter="True" HorizontalAlign="Center"> 
<Columns> 
    <asp:TemplateField HeaderText=" Function"> 
     <ItemTemplate> 
      <asp:Label Width="150px" ID="Function" ItemStyle-HorizontalAlign="Center" runat="server" Text='<%# Bind("Function") %>'></asp:Label> 
      <asp:GridView ID="StaffInfo" AutoGenerateColumns="false" runat="server"> 
       <Columns> 
        <asp:BoundField ItemStyle-Width="150px" DataField="FirstName" HeaderText="First Name" /> 
        <asp:BoundField ItemStyle-Width="150px" DataField="LastName" HeaderText="Last Name" /> 
        <asp:BoundField ItemStyle-Width="150px" DataField="SOEID" HeaderText="SOEID" /> 
       </Columns> 
      </asp:GridView>     
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText=" Team"> 
     <ItemTemplate> 
      <asp:Label Width="150px" ID="Team" ItemStyle-HorizontalAlign="Center" runat="server" Text='<%# Bind("Team") %>'></asp:Label> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="Staff Count"> 
     <ItemTemplate> 
      <asp:Button Width="40px" ID="StaffCount" ItemStyle-HorizontalAlign="Center" runat="server" Text='<%# Bind("StaffCount") %>' CommandName="ShowDetails" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" CausesValidation="True" UseSubmitBehavior="False" />     
     </ItemTemplate> 
      <FooterTemplate> 
       <asp:Button id="TotalStaff" runat="server" Text="Button1" CommandName="ShowAll" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CausesValidation="True" UseSubmitBehavior="False" /> 
      </FooterTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField Visible ="false"> 
     <ItemTemplate> 
      <asp:Label runat="server" Width="150px" DataField="TeamID" HeaderText="TeamID" ></asp:Label> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField Visible ="false"> 
     <ItemTemplate> 
      <asp:Label runat="server" ItemStyle-Width="150px" DataField="CityID" HeaderText="CityID"></asp:Label> 
     </ItemTemplate> 
    </asp:TemplateField> 
</Columns> 

回答

3

您可以通過下面的代碼 -

gridview1.FooterRow // Use name of your gridview 

檢查是否有任何IDEX財產,這樣你們可以得到頁腳行的索引獲得腳註行。

可能gridview1.FooterRow.RowIndex // not tested

+0

在這種情況下:INT指數= Staff.FooterRow.RowIndex;該指數也是-1。我的目標是單擊TotalStaff按鈕並將gridview綁定到頁腳,我將顯示所有員工。它適用於「常規」行的情況,但在腳註的情況下不起作用(因爲rowindex爲-1)。你有沒有想法,我怎麼能做到這一點? – ironcurtain

0

你可以試試這個:

var rowIndexForFooter = gridview.Rows.Count - 1; 

希望這有助於...