Repeater排序
- repeater
<asp:Repeater ID="rptGroups" runat="server" OnItemDataBound="rptGroups_ItemDataBound" OnItemCommand="rptGroups_ItemCommand">
- rd
<td> <asp:LinkButton ID="lbname" Runat="server" text="名称" CommandName="lbname"></asp:LinkButton> </td> <td> <asp:LinkButton ID="lbdate" Runat="server" text="时间" CommandName="lbdate"></asp:LinkButton> </td> <td> <asp:LinkButton ID="lbdays" Runat="server" text="天数" CommandName="lbdays"></asp:LinkButton> </td>
- ItemDataBound
protected void rptGroups_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Header) { if (ViewState["id"] != null) { LinkButton lkbtnSort = (LinkButton)e.Item.FindControl(ViewState["id"].ToString().Trim()); lkbtnSort.Text = ViewState["text"].ToString(); } } }
- ItemCommand
protected void rptGroups_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.Item.ItemType == ListItemType.Header) { LinkButton lkbtnSort = (LinkButton)e.Item.FindControl(e.CommandName.Trim()); if (ViewState[e.CommandName.Trim()] == null) { ViewState[e.CommandName.Trim()] = "ASC"; lkbtnSort.Text = lkbtnSort.Text + "↑"; } else { if (ViewState[e.CommandName.Trim()].ToString().Trim() == "ASC") { ViewState[e.CommandName.Trim()] = "DESC"; if (lkbtnSort.Text.IndexOf("↑") != -1) lkbtnSort.Text = lkbtnSort.Text.Replace("↑", "↓"); else lkbtnSort.Text = lkbtnSort.Text + "↓"; } else { ViewState[e.CommandName.Trim()] = "ASC"; if (lkbtnSort.Text.IndexOf("↓") != -1) lkbtnSort.Text = lkbtnSort.Text.Trim().Replace("↓", "↑"); else lkbtnSort.Text = lkbtnSort.Text + "↑"; } } ViewState["text"] = lkbtnSort.Text; ViewState["id"] = e.CommandName.Trim(); IList<Model.DJ_TourGroup> tglist = (IList<Model.DJ_TourGroup>)rptGroups.DataSource; switch (e.CommandName.Trim()) { case "lbname": if (ViewState[e.CommandName.Trim()].ToString().Trim() == "ASC") tglist = tglist.OrderBy(x => x.Name).ToList(); else tglist = tglist.OrderByDescending(x => x.Name).ToList(); break; case "lbdate": if (ViewState[e.CommandName.Trim()].ToString().Trim() == "ASC") tglist = tglist.OrderBy(x => x.BeginDate).ToList(); else tglist = tglist.OrderByDescending(x => x.BeginDate).ToList(); break; case "lbdays": if (ViewState[e.CommandName.Trim()].ToString().Trim() == "ASC") tglist = tglist.OrderBy(x => x.DaysAmount).ToList(); else tglist = tglist.OrderByDescending(x => x.DaysAmount).ToList(); break; } rptGroups.DataSource = tglist; rptGroups.DataBind(); } }
TAG: