EPR类企业管理系统

在我们现有系统基础上或全新开发,提供定制服务
为您的企业高效快速实施ERP,WMS,MES,CRM管理系统
全面管控物料仓库、销售业务、采购业务、仓库业务
生产过程、质量检验、组织架构、业务报表


定制
QQ:460-3528

开发
QQ群:3360-90194

源码
微信:136-3650-3721

如何:从客户端事件引发XAF回调并在服务器上处理这些回调

本主题描述如何从客户端事件引发XAF回调并在服务器上处理这些事件。如果您不能使用默认的控件回调来实现某种行为,并且需要刷新页面或显示弹出窗口,请使用此方法。

提示

DevExpress代码示例数据库(http://www.devexpress.com/example=E4087)中提供了完整的示例项目。

用户单击此单元格时,请按照以下步骤显示“数据透视网格”单元格的相关对象列表。您可以对任何其他Web控件使用类似的方法。

  • 在ASP.NET模块项目中,创建一个ViewController并实现CallbackManager属性,该属性返回XafCallbackManager对象。

    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.Web;
    using DevExpress.ExpressApp.Web.Templates;
    // ...
    public class PivotDrillDownController : ViewController<ListView> {
        protected XafCallbackManager CallbackManager {
            get { return ((ICallbackManagerHolder)WebWindow.CurrentRequestPage).CallbackManager; }
        }
    }
    
  • 重写OnViewControlsCreated方法,访问ASPxPivotGrid控件,并为客户端ASPxClientPivotGrid.CellClick事件注册一个新脚本。要创建脚本,请使用带有以下参数的XafCallbackManager.GetScript方法:

    范围 描述
    handlerId 注册处理程序的标识符。该值对于每个处理程序实例都必须是唯一的。您可以使用Guid.NewGuidObject.GetHashCode方法来创建唯一的handlerId值。
    参数 调用XAF回调时从客户端传递的字符串值。在此示例中,传递了单元格的ColumnIndexRowIndex值。如果不需要参数,则可以传递一个空字符串。
    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.PivotGrid.Web;
    using DevExpress.ExpressApp.Web;
    using DevExpress.ExpressApp.Web.Templates;
    using DevExpress.ExpressApp.Web.Utils;
    using DevExpress.Web.ASPxPivotGrid;
    // ...
    public class PivotDrillDownController : ViewController<ListView> {
        // ...
        private readonly string handlerId;
        protected override void OnViewControlsCreated() {
            base.OnViewControlsCreated();
            ASPxPivotGridListEditor pivotGridListEditor = View.Editor as ASPxPivotGridListEditor;
            if(pivotGridListEditor != null) {
                ASPxPivotGrid pivotGrid = pivotGridListEditor.PivotGridControl;
                string script = CallbackManager.GetScript(handlerId, "e.ColumnIndex   ';'   e.RowIndex");
                ClientSideEventsHelper.AssignClientHandlerSafe(pivotGrid, "CellClick", "function(s, e) {"   script   "}", "PivotDrillDownController");
            }
        }
        public PivotDrillDownController() {
            handlerId = "PivotDrillDownHandler"   GetHashCode();
        }
    }
    
    注意

    使用ClientSideEventsHelper.AssignClientHandlerSafe方法注册客户端事件处理程序脚本,并避免替换处理相同客户端事件的现有脚本。

  • 在Controller中实现IXafCallbackHandler接口。要注册XAF回调处理程序,请重写OnViewControlCreated方法并调用XafCallbackManager.RegisterHandler方法。第一个参数是之前创建的handlerId值。第二个参数是IXafCallbackHandler对象(当前的Controller实例)。

    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.Web.Templates;
    // ...
    public class PivotDrillDownController : ViewController<ListView>, IXafCallbackHandler {
        // ...
        private readonly string handlerId;
        protected override void OnViewControlsCreated() {
            base.OnViewControlsCreated();
            CallbackManager.RegisterHandler(handlerId, this);
            // ...
        }
        void IXafCallbackHandler.ProcessAction(string parameter) { 
        }
        // ...
    }
    
    注意
    • OnViewControlsCreated方法中注册XAF回调处理程序,因为在处理每个页面回调之前都会调用此方法。
    • 如果启用了WebApplication.OptimizationSettings.AllowFastProcessListViewRecordActions选项,则在自定义ListViewShowObject和“编辑操作”时不会调用OnViewControlsCreated方法。有关如何禁用此选项的详细信息,请参阅XAF KB中针对流行的Web UI场景的更快的渲染和其他性能优化。
  • 将接收XAF回调时服务器执行的代码添加到ProcessAction方法中。例如,从传递的参数中获取单击的单元格的列和行索引,检索用于计算此单元格摘要的记录,然后在“列表视图”中显示这些记录。

    using System;
    using System.Collections;
    using System.Linq;
    using DevExpress.Data.Filtering;
    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.PivotGrid.Web;
    using DevExpress.ExpressApp.Web.Templates;
    using DevExpress.XtraPivotGrid;
    // ...
    void IXafCallbackHandler.ProcessAction(string parameter) {
        string[] indices = parameter.Split(';');
        int columnIndex = Int32.Parse(indices[0]);
        int rowIndex = Int32.Parse(indices[1]);
        PivotDrillDownDataSource drillDown = ((ASPxPivotGridListEditor)View.Editor).PivotGridControl.CreateDrillDownDataSource(columnIndex, rowIndex);
        string name = View.ObjectTypeInfo.KeyMember.Name;
        IList keysToShow = drillDown.Cast<PivotDrillDownDataRow>().Where(row => row[name] != null).Select(row => row[name]).ToList();            
        if (keysToShow.Count > 0) {
            Type targetType = View.ObjectTypeInfo.Type;
            string viewId = Application.GetListViewId(targetType);
            CollectionSourceBase collectionSource = Application.CreateCollectionSource(Application.CreateObjectSpace(targetType), targetType, viewId);
            collectionSource.Criteria["SelectedObjects"] = new InOperator(ObjectSpace.GetKeyPropertyName(targetType), keysToShow);
            ListView listView = Application.CreateListView(viewId, collectionSource, false);
            Application.ShowViewStrategy.ShowViewInPopupWindow(listView);
        }
    }
    

转载保留此链接,注明出处