多重表头之排序

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

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

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

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

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

ASPX

 <script language="javascript" type="text/javascript">
function test(o)
{
document.getElementById("Label1").innerText = o;
document.getElementById("LinkButton1").click();
} </script>
.... <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowSorting="true"
onrowcreated="GridView1_RowCreated">
<Columns>
<asp:BoundField DataField="StoreId" HeaderText="Store Id" SortExpression="StoreId" />
<asp:BoundField DataField="Requeseter" HeaderText="Requester" SortExpression="Requeseter" />
<asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" SortExpression="EmployeeId"/>
<asp:BoundField DataField="StoreId" HeaderText="StoreId2" SortExpression="StoreId" />
</Columns>
</asp:GridView> <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click"
ClientIDMode="Static">LinkButton</asp:LinkButton>
<asp:HiddenField ID="Label1" runat="server" Value=""/>

CS

         protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Mix> list = new List<Mix>();
int i = ;
list.AddRange(new List<Mix> { new Mix { StoreId = , Requeseter = "A", EmployeeId = i + }, new Mix { StoreId = , Requeseter = "A", EmployeeId = i + }, new Mix { StoreId = , Requeseter = "A", EmployeeId = i + }, new Mix { StoreId = , Requeseter = "B", EmployeeId = i + } });
ViewState["lst"] = list;
GridView1.DataSource = list;
GridView1.DataBind();
ViewState["i"] = ;
}
this.LinkButton1.Style.Add("display", "none");
} protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
case DataControlRowType.Header: TableCellCollection tcHeader = e.Row.Cells;
tcHeader.Clear();
//Associate
tcHeader.Add(new TableHeaderCell());
tcHeader[].BackColor = System.Drawing.Color.FromArgb(, , );
tcHeader[].ForeColor = System.Drawing.Color.White;
tcHeader[].BorderColor = System.Drawing.Color.LightGray;
tcHeader[].HorizontalAlign = HorizontalAlign.Center;
tcHeader[].Attributes.Add("rowspan", "");
tcHeader[].Text = "<br><a id='Associate' href=\"#\" onclick='test(this.id)'>Associate</a>";
// ClientScriptManager.RegisterForEventValidation();
//SecurityLevel
tcHeader.Add(new TableHeaderCell());
tcHeader[].BackColor = System.Drawing.Color.FromArgb(, , );
tcHeader[].ForeColor = System.Drawing.Color.White;
tcHeader[].BorderColor = System.Drawing.Color.LightGray;
tcHeader[].HorizontalAlign = HorizontalAlign.Center;
tcHeader[].Attributes.Add("colspan", "");
tcHeader[].Text = "<p>SecurityLevel</p></th></tr><tr>"; //2Line: Default Level
tcHeader.Add(new TableHeaderCell());
tcHeader[].BackColor = System.Drawing.Color.FromArgb(, , );
tcHeader[].ForeColor = System.Drawing.Color.White;
tcHeader[].BorderColor = System.Drawing.Color.LightGray;
tcHeader[].HorizontalAlign = HorizontalAlign.Center;
tcHeader[].Text = "<a id='jobcodedefault' href=\"#\" onclick='test(this.id)'>Job Code Default</a>";
//tcHeader[2].Text = "<p>Job Code Default</p>"; //2Line: Current Level
tcHeader.Add(new TableHeaderCell());
tcHeader[].BackColor = System.Drawing.Color.FromArgb(, , );
tcHeader[].ForeColor = System.Drawing.Color.White;
tcHeader[].BorderColor = System.Drawing.Color.LightGray;
tcHeader[].HorizontalAlign = HorizontalAlign.Center;
//<a id='jobcodedefault' href=\"#\" onclick='test(this.id)'>Job Code Default</a>
tcHeader[].Text = "<a id='current' href=\"#\" onclick='test(this.id)'>Current</a>"; //tcHeader[3].Text = "<label style='width:99%; text-align:center; vertical-align:middle; color:White; font-weight:bold '>Current</label>";
tcHeader.Add(new TableHeaderCell()); //2Line: Requested Level
tcHeader[].BackColor = System.Drawing.Color.FromArgb(, , );
tcHeader[].ForeColor = System.Drawing.Color.White;
tcHeader[].BorderColor = System.Drawing.Color.LightGray;
tcHeader[].HorizontalAlign = HorizontalAlign.Center;
tcHeader[].Style.Add("BorderColor", "White");
// tcHeader[4].Text = "<label style='width:99%; text-align:center; vertical-align:middle; color:White; font-weight:bold '>Requested</label></tr>";
tcHeader[].Text = "<a id='requester' href=\"#\" onclick='test(this.id)'>Requester</a></tr>"; break;
} } protected void LinkButton1_Click(object sender, EventArgs e)
{
List<Mix> list = new List<Mix>();
List<Mix> listSorted = new List<Mix>(); list = (List<Mix>)ViewState["lst"]; switch (Label1.Value)
{
case "Associate":
{
int j = (int)ViewState["i"]; if ((j++) % == )
{
var historydesc = from c in list orderby c.StoreId descending select c;
foreach (var x in historydesc)
{
listSorted.Add(x);
}
}
else
{
var historyasc = from c in list orderby c.StoreId ascending select c;
foreach (var x in historyasc)
{
listSorted.Add(x);
}
}
ViewState["i"] = j; break;
}
case "jobcodedefault":
{
int j = (int)ViewState["i"]; if ((j++) % == )
{
var historydesc= from c in list orderby c.Requeseter descending select c;
foreach (var x in historydesc)
{
listSorted.Add(x);
}
}
else
{
var historyasc = from c in list orderby c.Requeseter ascending select c;
foreach (var x in historyasc)
{
listSorted.Add(x);
}
}
ViewState["i"] = j; break;
}
case "current":
{
int j = (int)ViewState["i"]; if ((j++) % == )
{
var historydesc = from c in list orderby c.EmployeeId descending select c;
foreach (var x in historydesc)
{
listSorted.Add(x);
}
}
else
{
var historyasc = from c in list orderby c.EmployeeId ascending select c;
foreach (var x in historyasc)
{
listSorted.Add(x);
}
}
ViewState["i"] = j; break; }
case "requester":
{
int j = (int)ViewState["i"]; if ((j++) % == )
{
var historydesc = from c in list orderby c.StoreId descending select c;
foreach (var x in historydesc)
{
listSorted.Add(x);
}
}
else
{
var historyasc = from c in list orderby c.StoreId ascending select c;
foreach (var x in historyasc)
{
listSorted.Add(x);
}
}
ViewState["i"] = j; break; }
} this.GridView1.DataSource = listSorted;
this.GridView1.DataBind();
}

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

lkbAssociate.Click += new EventHandler(grdHistorySort);

以Associate列为栗子。

         //flag for asc and desc
private bool AssociateAsc
{
get { return (ViewState["AssociateAsc"] == null) ? true : (bool)ViewState["AssociateAsc"]; }
set { ViewState["AssociateAsc"] = value; }
}
        tcHeader.Add(new TableHeaderCell());
tcHeader[].BackColor = System.Drawing.Color.FromArgb(, , );
tcHeader[].ForeColor = System.Drawing.Color.White;
tcHeader[].BorderColor = System.Drawing.Color.LightGray;
tcHeader[].HorizontalAlign = HorizontalAlign.Center;
tcHeader[].Attributes.Add("rowspan", "");
LinkButton lkbAssociate = new LinkButton();
lkbAssociate.Style.Add("text-align", "center");
lkbAssociate.Style.Add("color", "white");
lkbAssociate.ID = "lkbAssociate";
lkbAssociate.Text = "Associate (EID)";
lkbAssociate.Click += new EventHandler(grdHistorySort);
tcHeader[].Controls.Add(lkbAssociate);
         protected void grdHistorySort(object sender, EventArgs e)
{
List<PMSecRequestHistoryInfo> originalList = new List<PMSecRequestHistoryInfo>();
List<PMSecRequestHistoryInfo> sortedList = new List<PMSecRequestHistoryInfo>(); originalList = (List<PMSecRequestHistoryInfo>)ViewState["InitHistoryTabList"];
LinkButton linkButonClicked = sender as LinkButton; switch (linkButonClicked.ID)
{ case "lkbAssociate":
{
if (ViewState["AssociateAsc"] == null || (bool)ViewState["AssociateAsc"])
{
var sorted = from c in originalList orderby c.DisplayName ascending select c;
foreach (var info in sorted)
{
sortedList.Add(info);
} ViewState["AssociateAsc"] = false;
}
else
{
var sorted = from c in originalList orderby c.DisplayName descending select c;
foreach (var info in sorted)
{
sortedList.Add(info);
} ViewState["AssociateAsc"] = true;
} }
break;
...

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

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

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

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

不闪的,才是健康的~

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

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

排序类似

         protected void LinkButton1_Click(object sender, EventArgs e)
{
List<PMSecRequestHistoryInfo> originalList = new List<PMSecRequestHistoryInfo>();
List<PMSecRequestHistoryInfo> sortedList = new List<PMSecRequestHistoryInfo>(); originalList = (List<PMSecRequestHistoryInfo>)ViewState["InitHistoryTabList"]; if (ViewState["RequestedLevelAsc"] == null || (bool)ViewState["RequestedLevelAsc"] == true)
{
var sorted = from c in originalList orderby c.RequestedSecurityLevel ascending select c;
foreach (var info in sorted)
{
sortedList.Add(info);
}
ViewState["RequestedLevelAsc"] = false; }
else
{
var sorted = from c in originalList orderby c.RequestedSecurityLevel descending select c;
foreach (var info in sorted)
{
sortedList.Add(info);
} ViewState["RequestedLevelAsc"] = true;
} grdHistoryRequest.DataSource = sortedList;
grdHistoryRequest.DataBind();
ViewState["InitHistoryTabList"] = sortedList; }

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

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

Gridview 多重表头 (二)的更多相关文章

  1. Gridview 多重表头 (一)

    今天看到一个人每个月更新博客,结果七年后改行去卖土特产...感慨良多... 虽然我也想去开餐厅~~ 今天需求里有一个多重表头,感觉比较奇特,特意留下记录,以防我的大脑被艾滋海默攻占~~没有女主的命,不 ...

  2. GridView合并表头多重表头

    后台代码: using System; using System.Data; using System.Configuration; using System.Web; using System.We ...

  3. GridView七十二绝技-大全(收藏版)(转至别人博客)

    快速预览:GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠 ...

  4. Silverlight多重表头实现

    效果: 实现主要逻辑:通过动态拼接XML生成表头样式,绑定到列上. 主要是动态拼接XML时要仔细核对对应的占位行,具体可以看代码,注释很详细 两个类一个接口 NTree<T>:定义表头树形 ...

  5. GridView合并表头、多重表头(转)

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { switch (e.Row.RowType) ...

  6. gridview自定义表头

    gridview为我们提供了丰富的接口,用于满足自定义需求. 通常asp:gridview会根据绑定的列Columns自动生成表头,展现在前台元素. 序号 类别 有时候需要复杂一些的表头. 序号 类别 ...

  7. GridView 自定义表头

    //修改表头 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { switch (e.Row.Ro ...

  8. 从头学起android&lt;GridView网格视图.二十八.&gt;

    GridView基于组件的网络格所有的形式的组分的,例如:当制作专辑,所有的图片将在相同的尺寸在不同的显示格在孩子,是否能够依靠这个组件完成.此组件的继承结构参见例如下面: java.lang.Obj ...

  9. Gridview 重建表头/单击单元格弹出对话框/改变单元格背景色

    整理工作~ 完整的代码在GitHub上, 路径: 项目背景:追踪某个issue,并且记录每天的状态. 要求:1.点击日期就能更改,并且用颜色标志不同的状态 2.增加按钮可关闭issue 3.布局要求日 ...

随机推荐

  1. 参数化SQL小认识

    在做机房收费系统项目,编写数据连接并访问数据库时,见别人都用了带“@”字符的SQL语句,就很好奇为什么都用这个语句呢?直接拼写SQL语句不是更加方便吗?带着这个问题上网查资料,才知道原来他们用的是参数 ...

  2. c# 调用 友盟api

    今天要使用友盟的推送API来给我的app进行推送信息,调试了好久,老是返回500错误,最终在友盟的技术人员支持下完成了此操作,在此多谢友盟技术和客服人员. 把发方法和注意事项贴出来供大家参考. pub ...

  3. C#参数传递、引用类型、值类型等的理解

    本博客不属于技术贴,主要是记录一些自己对不懂得地方的理解和学习的记录,请带着批判的眼光阅读~ 值类型存储在栈上,引用类型存储在堆上.栈是由高到低存储的,遵循先进后出的原则,是内存提前分配好的区域,内存 ...

  4. div没有设置高度时背景颜色不显示(浮动)

    在使用div+css进行网页布局时,如果外部div有背景颜色或者边框,而不设置其高度,在IE浏览器下显示正常.但是使用Firefox/opera浏览时却出现最外层Div的背景颜色和边框不起作用的问题. ...

  5. codeforces 342D Xenia and Dominoes(状压dp+容斥)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Xenia and Dominoes Xenia likes puzzles ...

  6. C++程序设计实践指导1.14字符串交叉插入改写要求实现

    改写要求:1:以指针为数据结构开辟存储空间 改写要求2:被插入字符串和插入字符串不等长,设计程序间隔插入 如被插入字符串长度为12,待插入字符串长度为5 则插入间隔为2 改写要求3:添加函数Inser ...

  7. python中json的操作示例

    先上一段示例 # -*- coding: cp936 -*- import json #构造一个示例数据,并打印成易读样式 j = {} j["userName"]="a ...

  8. Js之History对象

    Window对象的history属性引用的是该窗口的History对象.History对象是用来把窗口的浏览历史用文档和文档状态列表的形式表示.History对象的length属牲表示浏览历史列表中的 ...

  9. http://codepen.io/zhou-yg/pen/NqgPmg 在线编辑器

    http://codepen.io/zhou-yg/pen/NqgPmg          在线编辑器

  10. 前端上将字符串用语音读出来只能在IE上运行 其他不行 代码极少

    先保存保存自己的笔记 有高手看到求指点 <script type="text/javascript"> var VoiceObj; try { VoiceObj = n ...