2016-11-29 74 views
0

我在GridView中使用自定義排序,該排序通過單擊標題文本以按該列進行排序來工作。現在我想在標題的文本旁邊添加一個小箭頭來指示排序的方向。我正在做這與CSS風格,它的作品。問題是,通過這樣做,之前設置的CSS樣式(在aspx文件中)就消失了。獲取GridView的列標題的css

爲了對付這個人會認爲這只是獲取當前CSS樣式字符串並追加新樣式的問題。說起來容易做起來難。

我已經試過:

gvDocs.HeaderStyle.CssClass; // returns an empty string 
gvDocs.HeaderRow.CssClass; // returns an empty string 
gvDocs.HeaderRow.Cells[i].CssClass; // returns an empty string 
gvDocs.HeaderRow.Cells[i].Attributes["class"]; // returns null 
LinkButton lb = gvDocs.HeaderRow.Cells[i].Controls[0] as LinkButton; 
lb.CssClass; // returns an empty string 
lb.Attributes["class"]; // returns null 

我試圖把代碼幾乎無處不在,但它總是相同的結果。

接下來是aspx文件

<asp:GridView ID="gvDocs" runat="server" CssClass="table" ... 
    <Columns> 
     <asp:BoundField DataField="doc_number" HeaderText="#" ReadOnly="True" SortExpression="doc_number" ShowHeader="True"> 
      <HeaderStyle CssClass="vert-align" /> 
      <ItemStyle CssClass="vert-align" /> 
     </asp:BoundField> 
... 

不是每個頭都有相同的CSS樣式所以我真的需要知道什麼是目前的風格並追加一個顯示箭頭的片段。

我更新的gcDocs_Sorting事件這樣的CSS樣式:

gvDocs.HeaderRow.Cells[GetColumnIndex(Session[GV_ORDERS_SORT_FIELD].ToString(), gvDocs)].CssClass += " sortDesc"; 
// or "sortAsc", depends on the result of a previous `if`. 

所以,我怎麼能得到列標題的CSS的?

回答

0

你可以讓GridView控件添加CSS類進行自動分揀

您可以在代碼中做到這一點:在你的GridView的標記

gvDocs.SortedAscendingHeaderStyle.CssClass = "sorted-asc"; 
gvDocs.SortedAscendingCellStyle.CssClass = "sorted-asc"; 
gvDocs.SortedDescendingHeaderStyle.CssClass = "sorted-desc"; 
gvDocs.SortedDescendingCellStyle.CssClass = "sorted-desc"; 

或者直接:

<asp:GridView ID="gvDocs" runat="server" 
    SortedAscendingHeaderStyle-CssClass="sorted-asc" 
    SortedAscendingCellStyle-CssClass="sorted-asc" 
    SortedDescendingHeaderStyle-CssClass="sorted-desc" 
    SortedDescendingCellStyle-CssClass="sorted-desc"> 
</asp:GridView> 
+0

即贏得」工作。這將樣式應用到所有標題,而不是單獨。 –

+0

我不太確定你的意思,它只會將該類添加到已排序的列中。 – Sam