【转】ASPxGridView 日期范围过滤扩展

使用方法:
Step 1:aspx页面中引用DateRange.js,代码清单如下:

   1: function ApplyFilter(dde, fieldname, dateFrom, dateTo, container) {
   2:     var d1 = dateFrom.GetText();
   3:     var d2 = dateTo.GetText();
   4:     if (d1 == "" || d2 == "")
   5:         return;
   6:     var newValue = d1 + " 至 " + d2;
   7:     var oldValue = dde.lastSuccessText;
   8:     dde.SetText(newValue);
   9:     if (newValue != oldValue)
  10:         container.AutoFilterByColumn(fieldname, dde.GetText());
  11: }
  12:  
  13: function RemoveFilter(dde, fieldname, container) {
  14:     dde.SetText('');
  15:     container.AutoFilterByColumn(fieldname, '');
  16: }
  17:  
  18: function OnDropDown(s, dateFrom, dateTo) {
  19:     var str = s.GetText();
  20:     if (str == "") {
  21:         dateFrom.SetDate(new Date(2011, 1, 1));
  22:         dateTo.SetDate(new Date());
  23:         return;
  24:     }
  25:     var d = str.split(" 至 ");
  26:     dateFrom.SetText(d[0]);
  27:     dateTo.SetText(d[1]);
  28: }

Step 2:App_Code中添加DateRange.cs,代码清单如下:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Web;
   4: using System.Web.UI;
   5: using System.Web.UI.WebControls;
   6: using DevExpress.Data.Filtering;
   7: using DevExpress.Web.ASPxEditors;
   8: using DevExpress.Web.ASPxGridView;
   9:  
  10: public class DateTemplate : ITemplate
  11: {
  12:     // client IDs of two ASPxDateEdit controls
  13:     public String cIdFrom;
  14:     public String cIdTo;
  15:     public String FieldName;
  16:     public ASPxGridView Container;
  17:  
  18:     public DateTemplate(ASPxGridView container, string fieldname)
  19:         : base()
  20:     {
  21:         Container = container;
  22:         FieldName = fieldname;
  23:     }
  24:  
  25:     public void InstantiateIn(Control container)
  26:     {
  27:         Table table = new Table();
  28:         container.Controls.Add(table);
  29:         TableRow row = new TableRow();
  30:         table.Rows.Add(row);
  31:  
  32:         TableCell cell = new TableCell();
  33:         row.Cells.Add(cell);
  34:         ASPxLabel lbl = new ASPxLabel();
  35:         lbl.ID = "lblFrom";
  36:         lbl.Text = "从:";
  37:         cell.Controls.Add(lbl);
  38:  
  39:         cell = new TableCell();
  40:         row.Cells.Add(cell);
  41:         ASPxDateEdit dateFrom = new ASPxDateEdit();
  42:         dateFrom.ID = "dateFrom";
  43:         dateFrom.EnableClientSideAPI = true;
  44:         cell.Controls.Add(dateFrom);
  45:  
  46:         row = new TableRow();
  47:  
  48:         table.Rows.Add(row);
  49:  
  50:         cell = new TableCell();
  51:         row.Cells.Add(cell);
  52:         lbl = new ASPxLabel();
  53:         lbl.ID = "lblTo";
  54:         lbl.Text = "到:";
  55:         cell.Controls.Add(lbl);
  56:  
  57:         cell = new TableCell();
  58:         row.Cells.Add(cell);
  59:         ASPxDateEdit dateTo = new ASPxDateEdit();
  60:         dateTo.ID = "dateTo";
  61:         dateTo.EnableClientSideAPI = true;
  62:         cell.Controls.Add(dateTo);
  63:  
  64:         cIdFrom = dateFrom.ClientID;
  65:         cIdTo = dateTo.ClientID;
  66:  
  67:         row = new TableRow();
  68:  
  69:         table.Rows.Add(row);
  70:  
  71:         cell = new TableCell();
  72:         cell.ColumnSpan = 2;
  73:         row.Cells.Add(cell);
  74:         ASPxHyperLink lnk = new ASPxHyperLink();
  75:         lnk.ID = "lnkApply";
  76:         lnk.Text = "应用";
  77:         lnk.NavigateUrl = "#";
  78:         lnk.ClientSideEvents.Click =
  79: String.Format("function (s, e) {{ {0}.HideDropDown(); ApplyFilter({0}, '{1}', {2}, {3}, {4}); }}",
  80:             container.NamingContainer.NamingContainer.ClientID, FieldName, cIdFrom, cIdTo, Container.ClientID);
  81:         cell.Controls.Add(lnk);
  82:  
  83:         Literal space = new Literal();
  84:         space.Text = "nbsp; ";
  85:         cell.Controls.Add(space);
  86:  
  87:         ASPxHyperLink lnkCancel = new ASPxHyperLink();
  88:         lnkCancel.ID = "lnkCancel";
  89:         lnkCancel.Text = "清除";
  90:         lnkCancel.NavigateUrl = "#";
  91:         lnkCancel.ClientSideEvents.Click =
  92: String.Format("function (s, e) {{ {0}.HideDropDown(); RemoveFilter({0}, '{1}', {2}); }}",
  93:             container.NamingContainer.NamingContainer.ClientID, FieldName, Container.ClientID);
  94:         cell.Controls.Add(lnkCancel);
  95:     }
  96: }
  97:  
  98:  
  99: public class DateRangeHandler
 100: {
 101:     DateTemplate dateTemplate = null;
 102:     List<string> FieldNames = null;
 103:  
 104:     public DateRangeHandler(ASPxGridView sender, string fieldname)
 105:     {
 106:         FieldNames = new List<string>();
 107:         FieldNames.Add(fieldname);
 108:         sender.AutoFilterCellEditorCreate += grid_AutoFilterCellEditorCreate;
 109:         sender.AutoFilterCellEditorInitialize += grid_AutoFilterCellEditorInitialize;
 110:         sender.ProcessColumnAutoFilter += grid_ProcessColumnAutoFilter;
 111:     }
 112:  
 113:     public DateRangeHandler(ASPxGridView sender, List<string> fieldnames)
 114:     {
 115:         FieldNames = fieldnames;
 116:         sender.AutoFilterCellEditorCreate += grid_AutoFilterCellEditorCreate;
 117:         sender.AutoFilterCellEditorInitialize += grid_AutoFilterCellEditorInitialize;
 118:         sender.ProcessColumnAutoFilter += grid_ProcessColumnAutoFilter;
 119:     }
 120:  
 121:     private void grid_AutoFilterCellEditorCreate(object sender, ASPxGridViewEditorCreateEventArgs e)
 122:     {
 123:         ASPxGridView grid = sender as ASPxGridView;
 124:         if (FieldNames.Contains(e.Column.FieldName))
 125:         {
 126:             DropDownEditProperties dde = new DropDownEditProperties();
 127:             dde.EnableClientSideAPI = true;
 128:  
 129:             dateTemplate = new DateTemplate(sender as ASPxGridView, e.Column.FieldName);
 130:  
 131:             dde.DropDownWindowTemplate = dateTemplate;
 132:             dde.Style.BorderLeft.BorderStyle = BorderStyle.Solid;
 133:             dde.Style.BorderRight.BorderStyle = BorderStyle.Solid;
 134:             dde.Style.BorderTop.BorderStyle = BorderStyle.Solid;
 135:             dde.Style.BorderBottom.BorderStyle = BorderStyle.Solid;
 136:             e.EditorProperties = dde;
 137:         }
 138:     }
 139:  
 140:     private void grid_AutoFilterCellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
 141:     {
 142:         ASPxGridView grid = sender as ASPxGridView;
 143:         if (FieldNames.Contains(e.Column.FieldName))
 144:         {
 145:             ASPxDropDownEdit dde = e.Editor as ASPxDropDownEdit;
 146:             //dde.ClientSideEvents.CloseUp = String.Format("function (s, e) {{ ApplyFilter(s,'{0}', {1}, {2}, {3}); }}",
 147:             //FieldName, dateTemplate.cIdFrom, dateTemplate.cIdTo, (sender as ASPxGridView).ClientID);
 148:             dde.ClientSideEvents.DropDown = String.Format("function (s, e) {{ OnDropDown(s, {0}, {1}, {2}); }}",
 149:                 dateTemplate.cIdFrom, dateTemplate.cIdTo, (sender as ASPxGridView).ClientID);
 150:             dde.ReadOnly = true;
 151:         }
 152:     }
 153:  
 154:     private void grid_ProcessColumnAutoFilter(object sender, ASPxGridViewAutoFilterEventArgs e)
 155:     {
 156:         if (e.Value == "")
 157:             return;
 158:         if (FieldNames.Contains(e.Column.FieldName))
 159:         {
 160:             if (e.Kind == GridViewAutoFilterEventKind.CreateCriteria)
 161:             {
 162:                 HttpContext.Current.Session["CollectionDateFilter_" + e.Column.FieldName] = e.Value;
 163:                 String[] dates = e.Value.Split(new string[] { " 至 " }, StringSplitOptions.RemoveEmptyEntries);
 164:                 DateTime dateFrom = Convert.ToDateTime(dates[0] + " 00:00:00"),
 165:                     dateTo = Convert.ToDateTime(dates[1] + " 23:59:59");
 166:                 e.Criteria = (new OperandProperty(e.Column.FieldName) >= dateFrom) 
 167:                              (new OperandProperty(e.Column.FieldName) <= dateTo);
 168:             }
 169:             else
 170:             {
 171:                 if (HttpContext.Current.Session["CollectionDateFilter_" + e.Column.FieldName] != null)
 172:                     e.Value = HttpContext.Current.Session["CollectionDateFilter_" + e.Column.FieldName].ToString();
 173:             }
 174:         }
 175:         else
 176:         {
 177:             if (e.Kind == GridViewAutoFilterEventKind.CreateCriteria)
 178:             {
 179:                 long dte = DateTime.Now.Ticks;
 180:                 e.Criteria = e.Criteria | (new OperandProperty(e.Column.FieldName) == dte);
 181:             }
 182:         }
 183:     }
 184: }

Step 3:页面.cs文件中添加如下代码:
单列方式:

   1:  
   2:     protected void Page_Load(object sender, EventArgs e)
   3:    {
   4:        DateRangeHandler h = new DateRangeHandler(grid, "创建时间");
   5:    }

多列方式:

   1:  
   2:     protected void Page_Load(object sender, EventArgs e)
   3:    {
   4:        List<string> fieldnames = new List<string>();
   5:        fieldnames.Add("创建时间");
   6:        fieldnames.Add("订货时间");
   7:        fieldnames.Add("出货时间");
   8:        fieldnames.Add("审核时间");
   9:        DateRangeHandler h = new DateRangeHandler(grid, fieldnames);
  10:    }
原文:http://smartsoft.5d6d.com/viewthread.php?tid=6727
posted @ 2011-06-09 23:16  Asharp  阅读(2113)  评论(1编辑  收藏  举报