简体   繁体   中英

Changing the footer text in GridView

I can't seem to be able to change the footer text. I've tried the sorted event as well but nothing happens. All I want to do is display status messages. Here is my code:

protected void PageSettings_Sorting(object sender, GridViewSortEventArgs e)
{
  if (((GridView)sender).EditIndex > -1)
  {
    e.Cancel = true;
  }
  else
  {
    // tried this on sorted aswell but can't change footer text
    GridViewRow row = ((GridView)sender).FooterRow as GridViewRow;
    Label lblStatus = new Label{ ID="lblStatus", Text="Sorting Column <b>\"" + e.SortExpression + "\" " + e.SortDirection + "</b>"};
    row.Cells[0].Text = "Hello World"; //.Controls.Add(lblStatus);            
  }
}

protected void PageSettings_RowCreated(object sender, GridViewRowEventArgs e)
{
  if (e.Row != null && e.Row.RowType == DataControlRowType.Header)
  {

  }
  else if (e.Row != null && e.Row.RowType == DataControlRowType.Footer)
  {
    int count = e.Row.Cells.Count;
    for (int i = count - 1; i >= 1; i += -1)
    {
      e.Row.Cells.RemoveAt(i);
    }
    e.Row.Cells[0].ColumnSpan = count;
    e.Row.Cells[0].Controls.Add(new Literal { ID = "lblStatus" });
    // can't FindControl or change Literals either
    e.Row.Cells[0].Text = "Hello World"; // works here but not on sorting event
  }        
}

<asp:GridView ID="PageSettings" runat="server"
     AllowPaging="true" AllowSorting="true"
     AutoGenerateColumns="false"
     AutoGenerateDeleteButton="true"
     AutoGenerateEditButton="true"
     ShowFooter="true" 
     DataSourceID="ObjectDataSourcePages"
     OnLoad="PageSettings_Load" 
     OnRowDataBound="PageSettings_DataBound"
     OnRowCreated="PageSettings_RowCreated"
     OnRowEditing="PageSettings_RowEditing" 
     OnRowCancelingEdit="PageSettings_RowCancelingEdit"      
     OnPageIndexChanging="PageSettings_PageIndexChanging"
     OnSorting="PageSettings_Sorting" 
     OnSorted="PageSetting_Sorted"    
     PageSize="2">        
  <Columns>
    <asp:TemplateField HeaderText="Page Name" HeaderStyle-HorizontalAlign="Left" SortExpression="Name">
      <ItemTemplate>
        <%# Eval("Name") %>
      </ItemTemplate>
      <EditItemTemplate>
        <asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
      </EditItemTemplate>                
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Path" HeaderStyle-HorizontalAlign="Left" SortExpression="Path">
      <ItemTemplate>
        <%# Eval("Path") %>
      </ItemTemplate>
      <EditItemTemplate>                    
        <asp:TextBox ID="Path" runat="server" Text='<%# Bind("Path") %>'></asp:TextBox>
      </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Route Value" HeaderStyle-HorizontalAlign="Left" SortExpression="RouteValue">
      <ItemTemplate>
        <%# Eval("RouteValue") %>
      </ItemTemplate>
      <EditItemTemplate>
        <asp:TextBox ID="RouteValue" runat="server" Text='<%# Bind("RouteValue") %>'></asp:TextBox>
      </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="RegExp" HeaderStyle-HorizontalAlign="Left" SortExpression="RegExp">
      <ItemTemplate>
        <%# Eval("RegExp") %>
      </ItemTemplate>
      <EditItemTemplate>
        <asp:TextBox ID="RegExp" runat="server" Text='<%# Bind("RegExp") %>'></asp:TextBox>
      </EditItemTemplate>
    </asp:TemplateField>
  </Columns>                
</asp:GridView>

This will change the text of the first FooterRow cell:

protected void gv_Sorting(object sender, GridViewSortEventArgs e)
{
    gv.FooterRow.Cells[0].Text = "Hello";
}

Of course you'll need to make sure your GridView 's ShowFooter property is true.

Or alternatively, by casting sender and adding a control:

protected void Sorting(object sender, GridViewSortEventArgs e)
{
    Label label = new Label();
    label.Text = gv_s.Rows.Count.ToString() + " records";
    ((GridView)sender).FooterRow.Cells[0].Controls.Add(label);
}

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