在GridView中更改页脚文本

时间:2012-01-07 13:09:33

标签: c# asp.net

我似乎无法更改页脚文本。我也尝试了排序事件,但没有任何反应。我想要做的就是显示状态消息。这是我的代码:

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>

1 个答案:

答案 0 :(得分:1)

这将更改第一个FooterRow单元格的文本:

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

当然,您需要确保GridView的{​​{1}}属性为真。

或者,通过转换发件人并添加控件:

ShowFooter