2012-01-07 69 views
1

我似乎無法更改頁腳文本。我也嘗試了排序的事件,但沒有任何反應。我想要做的就是顯示狀態消息。這裏是我的代碼:在GridView中更改頁腳文本

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

郵政標記,所以我們可以證明/確定哪些內容頁腳了。 – adatapost 2012-01-07 13:12:17

+0

爲什麼你想在你的整理事件中做到這一點?當然你想要在創建/綁定事件中完成它? – Brissles 2012-01-07 15:45:13

回答

1

這將改變第一FooterRow單元格的文本:

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

當然,你需要確保你的GridViewShowFooter屬性爲true。

或者,通過鑄造發送者和添加控制:GridView控件的

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); 
} 
+0

這就是我的例子已經得到了什麼。它不起作用 – ONYX 2012-01-08 03:37:42

+0

它在我的工作。我假設你已經嘗試啓動排序事件來查看結果? – Brissles 2012-01-08 04:44:14

+0

由於某種原因,我的頁腳不會將文本添加到任何單元格中任何想法 – ONYX 2012-01-08 05:14:57