• Gridview 多重表头 (二)


    多重表头之排序

    这是个有点忧桑的故事。。。Cynthia告诉我,研究一个问题,我们不可能有超过一天的时间。。。

    结果好好几天过去鸟~~还没有完成。。。

    由于不再使用Gridview自带的表头行,于是无法绑定gridview自带排序方法。只能根据点击列名做不同处理。

    我的思路是酱滴,将点击的列名存在一个hiddenfield里,排序时候根据hiddenfield里存储的值作为升/降序的依据。

    好丢人的说,第一次的时候存在了label里,每次回传都被label默认值覆盖,后来才发现,不是所有控件都叫Hidden field...

    ASPX

     1 <script language="javascript" type="text/javascript">
     2     function test(o) 
     3     {
     4         document.getElementById("Label1").innerText = o;
     5         document.getElementById("LinkButton1").click();
     6     }
     7 
     8 </script>
     9                                  ....
    10 
    11       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  AllowSorting="true"
    12             onrowcreated="GridView1_RowCreated">
    13           <Columns>
    14               <asp:BoundField DataField="StoreId" HeaderText="Store Id" SortExpression="StoreId" />
    15               <asp:BoundField DataField="Requeseter" HeaderText="Requester" SortExpression="Requeseter" />
    16               <asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId"  SortExpression="EmployeeId"/>
    17               <asp:BoundField DataField="StoreId" HeaderText="StoreId2" SortExpression="StoreId"  />
    18           </Columns>
    19     </asp:GridView>
    20 
    21         <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" 
    22             ClientIDMode="Static">LinkButton</asp:LinkButton>
    23          <asp:HiddenField ID="Label1" runat="server" Value=""/>
    View Code


    CS

      1         protected void Page_Load(object sender, EventArgs e)
      2         {
      3             if (!IsPostBack)
      4             {
      5                 List<Mix> list = new List<Mix>();
      6                 int i = 1000;
      7                 list.AddRange(new List<Mix> { new Mix { StoreId = 1, Requeseter = "A", EmployeeId = i + 3 }, new Mix { StoreId = 3, Requeseter = "A", EmployeeId = i + 2 }, new Mix { StoreId = 2, Requeseter = "A", EmployeeId = i + 4 }, new Mix { StoreId = 4, Requeseter = "B", EmployeeId = i + 3 } });
      8                 ViewState["lst"] = list;
      9                 GridView1.DataSource = list;
     10                 GridView1.DataBind();
     11                 ViewState["i"] = 1;
     12             }
     13             this.LinkButton1.Style.Add("display", "none");
     14         }
     15 
     16         protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
     17         {
     18             switch (e.Row.RowType)
     19             {
     20                 case DataControlRowType.Header:
     21 
     22                     TableCellCollection tcHeader = e.Row.Cells;
     23                     tcHeader.Clear();
     24                     //Associate
     25                     tcHeader.Add(new TableHeaderCell());
     26                     tcHeader[0].BackColor = System.Drawing.Color.FromArgb(99, 74, 141);
     27                     tcHeader[0].ForeColor = System.Drawing.Color.White;
     28                     tcHeader[0].BorderColor = System.Drawing.Color.LightGray;
     29                     tcHeader[0].HorizontalAlign = HorizontalAlign.Center;
     30                     tcHeader[0].Attributes.Add("rowspan", "2");
     31                     tcHeader[0].Text = "<br><a  id='Associate' href="#" onclick='test(this.id)'>Associate</a>";
     32                     //   ClientScriptManager.RegisterForEventValidation();
     33                     //SecurityLevel
     34                     tcHeader.Add(new TableHeaderCell());
     35                     tcHeader[1].BackColor = System.Drawing.Color.FromArgb(99, 74, 141);
     36                     tcHeader[1].ForeColor = System.Drawing.Color.White;
     37                     tcHeader[1].BorderColor = System.Drawing.Color.LightGray;
     38                     tcHeader[1].HorizontalAlign = HorizontalAlign.Center;
     39                     tcHeader[1].Attributes.Add("colspan", "3");
     40                     tcHeader[1].Text = "<p>SecurityLevel</p></th></tr><tr>";
     41 
     42                     //2Line: Default Level
     43                     tcHeader.Add(new TableHeaderCell());
     44                     tcHeader[2].BackColor = System.Drawing.Color.FromArgb(99, 74, 141);
     45                     tcHeader[2].ForeColor = System.Drawing.Color.White;
     46                     tcHeader[2].BorderColor = System.Drawing.Color.LightGray;
     47                     tcHeader[2].HorizontalAlign = HorizontalAlign.Center;
     48                     tcHeader[2].Text = "<a  id='jobcodedefault' href="#" onclick='test(this.id)'>Job Code Default</a>";
     49                     //tcHeader[2].Text = "<p>Job Code Default</p>";
     50 
     51                     //2Line: Current Level
     52                     tcHeader.Add(new TableHeaderCell());
     53                     tcHeader[3].BackColor = System.Drawing.Color.FromArgb(99, 74, 141);
     54                     tcHeader[3].ForeColor = System.Drawing.Color.White;
     55                     tcHeader[3].BorderColor = System.Drawing.Color.LightGray;
     56                     tcHeader[3].HorizontalAlign = HorizontalAlign.Center;
     57                     //<a  id='jobcodedefault' href="#" onclick='test(this.id)'>Job Code Default</a>
     58                     tcHeader[3].Text = "<a  id='current' href="#" onclick='test(this.id)'>Current</a>";
     59 
     60                     //tcHeader[3].Text = "<label style='99%; text-align:center; vertical-align:middle; color:White; font-weight:bold '>Current</label>";
     61                     tcHeader.Add(new TableHeaderCell());
     62 
     63                     //2Line: Requested Level
     64                     tcHeader[4].BackColor = System.Drawing.Color.FromArgb(99, 74, 141);
     65                     tcHeader[4].ForeColor = System.Drawing.Color.White;
     66                     tcHeader[4].BorderColor = System.Drawing.Color.LightGray;
     67                     tcHeader[4].HorizontalAlign = HorizontalAlign.Center;
     68                     tcHeader[4].Style.Add("BorderColor", "White");
     69                     //      tcHeader[4].Text = "<label style='99%; text-align:center; vertical-align:middle; color:White; font-weight:bold '>Requested</label></tr>";
     70                     tcHeader[4].Text = "<a  id='requester' href="#" onclick='test(this.id)'>Requester</a></tr>";
     71 
     72                     break;
     73             }
     74 
     75 
     76         }
     77 
     78 
     79 
     80         protected void LinkButton1_Click(object sender, EventArgs e)
     81         {
     82             List<Mix> list = new List<Mix>();
     83             List<Mix> listSorted = new List<Mix>();
     84 
     85             list = (List<Mix>)ViewState["lst"];
     86 
     87             switch (Label1.Value)
     88             {
     89                 case "Associate":
     90                     {
     91                         int j = (int)ViewState["i"];
     92 
     93                         if ((j++) % 2 == 0)
     94                         {
     95                             var historydesc = from c in list orderby c.StoreId descending select c;
     96                             foreach (var x in historydesc)
     97                             {
     98                                 listSorted.Add(x);
     99                             }
    100                         }
    101                         else
    102                         {
    103                             var historyasc = from c in list orderby c.StoreId ascending select c;
    104                             foreach (var x in historyasc)
    105                             {
    106                                 listSorted.Add(x);
    107                             }
    108                         }
    109                         ViewState["i"] = j;
    110 
    111                         break;
    112                     }
    113                 case "jobcodedefault":
    114                     {
    115                         int j = (int)ViewState["i"];
    116 
    117                         if ((j++) % 2 == 0)
    118                         {
    119                             var historydesc= from c in list orderby c.Requeseter descending select c;
    120                             foreach (var x in historydesc)
    121                             {
    122                                 listSorted.Add(x);
    123                             }
    124                         }
    125                         else
    126                         {
    127                             var historyasc = from c in list orderby c.Requeseter ascending select c;
    128                             foreach (var x in historyasc)
    129                             {
    130                                 listSorted.Add(x);
    131                             }
    132                         }
    133                         ViewState["i"] = j;
    134 
    135                         break;
    136                     }
    137                 case "current":
    138                     {
    139                         int j = (int)ViewState["i"];
    140 
    141                         if ((j++) % 2 == 0)
    142                         {
    143                             var historydesc = from c in list orderby c.EmployeeId descending select c;
    144                             foreach (var x in historydesc)
    145                             {
    146                                 listSorted.Add(x);
    147                             }
    148                         }
    149                         else
    150                         {
    151                             var historyasc = from c in list orderby c.EmployeeId ascending select c;
    152                             foreach (var x in historyasc)
    153                             {
    154                                 listSorted.Add(x);
    155                             }
    156                         }
    157                         ViewState["i"] = j;
    158 
    159                         break;
    160 
    161                     }
    162                 case "requester":
    163                     {
    164                         int j = (int)ViewState["i"];
    165 
    166                         if ((j++) % 2 == 0)
    167                         {
    168                             var historydesc = from c in list orderby c.StoreId descending select c;
    169                             foreach (var x in historydesc)
    170                             {
    171                                 listSorted.Add(x);
    172                             }
    173                         }
    174                         else
    175                         {
    176                             var historyasc = from c in list orderby c.StoreId ascending select c;
    177                             foreach (var x in historyasc)
    178                             {
    179                                 listSorted.Add(x);
    180                             }
    181                         }
    182                         ViewState["i"] = j;
    183 
    184                         break;
    185 
    186                     }
    187             }
    188 
    189 
    190             this.GridView1.DataSource = listSorted;
    191             this.GridView1.DataBind();
    192         }
    View Code

    就算是简单的实现了吧,直到Cynthia告诉我可以酱。

    lkbAssociate.Click += new EventHandler(grdHistorySort);

    以Associate列为栗子。

    1         //flag for asc and desc
    2         private bool AssociateAsc
    3         {
    4             get { return (ViewState["AssociateAsc"] == null) ? true : (bool)ViewState["AssociateAsc"]; }
    5             set { ViewState["AssociateAsc"] = value; }
    6         }
    View Code
     1        tcHeader.Add(new TableHeaderCell());
     2                         tcHeader[1].BackColor = System.Drawing.Color.FromArgb(99, 74, 141);
     3                         tcHeader[1].ForeColor = System.Drawing.Color.White;
     4                         tcHeader[1].BorderColor = System.Drawing.Color.LightGray;
     5                         tcHeader[1].HorizontalAlign = HorizontalAlign.Center;
     6                         tcHeader[1].Attributes.Add("rowspan", "2");
     7                         LinkButton lkbAssociate = new LinkButton();
     8                         lkbAssociate.Style.Add("text-align", "center");
     9                         lkbAssociate.Style.Add("color", "white");
    10                         lkbAssociate.ID = "lkbAssociate";
    11                         lkbAssociate.Text = "Associate (EID)";
    12                         lkbAssociate.Click += new EventHandler(grdHistorySort);
    13                         tcHeader[1].Controls.Add(lkbAssociate);
    View Code
     1         protected void grdHistorySort(object sender, EventArgs e)
     2         {
     3             List<PMSecRequestHistoryInfo> originalList = new List<PMSecRequestHistoryInfo>();
     4             List<PMSecRequestHistoryInfo> sortedList = new List<PMSecRequestHistoryInfo>();
     5 
     6             originalList = (List<PMSecRequestHistoryInfo>)ViewState["InitHistoryTabList"];
     7             LinkButton linkButonClicked = sender as LinkButton;
     8 
     9             switch (linkButonClicked.ID)
    10             {
    11              
    12                 case "lkbAssociate":
    13                     {
    14                         if (ViewState["AssociateAsc"] == null || (bool)ViewState["AssociateAsc"])
    15                         {
    16                             var sorted = from c in originalList orderby c.DisplayName ascending select c;
    17                             foreach (var info in sorted)
    18                             {
    19                                 sortedList.Add(info);
    20                             }
    21 
    22                             ViewState["AssociateAsc"] = false;
    23                         }
    24                         else
    25                         {
    26                             var sorted = from c in originalList orderby c.DisplayName descending select c;
    27                             foreach (var info in sorted)
    28                             {
    29                                 sortedList.Add(info);
    30                             }
    31 
    32                             ViewState["AssociateAsc"] = true;
    33                         }
    34 
    35                     }
    36                     break;
    37                    ...
    View Code


    酱婶儿的,会不会简明扼要的多。。。Cynthiao(≧v≦)o~~好棒

    唯一遗憾的是我不晓得如何为分隔的列添加控件同时添加格式。

    这样两列,不能如法炮制。

    第一次时候用了上次的写法,写了一个<a onclik></a>,发现页面要刷瞎我的双眼。。。

    不闪的,才是健康的~

    于是,拽了一个LinkButton,点了事件里的onclick想看看是个什么样子。ViewSource 发现是辣样一个东西,于是模仿了一个。

    1                         tcHeader[12].Text = "<a id='LinkButton1' href='javascript:__doPostBack(&#39;LinkButton1&#39;,&#39;&#39;)' style='text-align:center;color:white' >Request</a></th></tr><tr>";
    View Code

    排序类似

     1         protected void LinkButton1_Click(object sender, EventArgs e)
     2         {
     3             List<PMSecRequestHistoryInfo> originalList = new List<PMSecRequestHistoryInfo>();
     4             List<PMSecRequestHistoryInfo> sortedList = new List<PMSecRequestHistoryInfo>();
     5 
     6             originalList = (List<PMSecRequestHistoryInfo>)ViewState["InitHistoryTabList"];
     7 
     8                 if (ViewState["RequestedLevelAsc"] == null || (bool)ViewState["RequestedLevelAsc"] == true)
     9                 {
    10                     var sorted = from c in originalList orderby c.RequestedSecurityLevel ascending select c;
    11                     foreach (var info in sorted)
    12                     {
    13                         sortedList.Add(info);
    14                     }
    15                     ViewState["RequestedLevelAsc"] = false;
    16 
    17                 }
    18                 else
    19                 {
    20                     var sorted = from c in originalList orderby c.RequestedSecurityLevel descending select c;
    21                     foreach (var info in sorted)
    22                     {
    23                         sortedList.Add(info);
    24                     }
    25 
    26                     ViewState["RequestedLevelAsc"] = true;
    27                 }
    28             
    29 
    30             grdHistoryRequest.DataSource = sortedList;
    31             grdHistoryRequest.DataBind();
    32             ViewState["InitHistoryTabList"] = sortedList;
    33 
    34 
    35         }
    View Code


    真正困惑我的是,如果我把页面上的LinkButton删掉,排序会 米!有!效!

    百思不得姐,姐百思不得。。。。

  • 相关阅读:
    innodb文件
    Innodb 存储引擎
    第二章 flex输入输出结构
    第二章 flex输入输出
    第二章 flex处理二义性
    第一章 flex单词计数程序
    Compile SQLite3 from individual files
    标 题: [心得]传统IT转互联网面试经验分享
    【设计模式】二:策略模式
    python 爬虫第三例--爬美女图片
  • 原文地址:https://www.cnblogs.com/coderinprague/p/4281671.html
Copyright © 2020-2023  润新知