商信互联
本主题描述如何从客户端事件引发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; }
}
}
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Web
Imports DevExpress.ExpressApp.Web.Templates
' ...
Public Class PivotDrillDownController
Inherits ViewController(Of ListView)
Protected ReadOnly Property CallbackManager() As XafCallbackManager
Get
Return (CType(WebWindow.CurrentRequestPage, ICallbackManagerHolder)).CallbackManager
End Get
End Property
End Class
重写OnViewControlsCreated方法,访问ASPxPivotGrid控件,并为客户端ASPxClientPivotGrid.CellClick事件注册一个新脚本。要创建脚本,请使用带有以下参数的XafCallbackManager.GetScript方法:
范围 | 描述 |
---|---|
handlerId | 注册处理程序的标识符。该值对于每个处理程序实例都必须是唯一的。您可以使用Guid.NewGuid或Object.GetHashCode方法来创建唯一的handlerId值。 |
参数 | 调用XAF回调时从客户端传递的字符串值。在此示例中,传递了单元格的ColumnIndex和RowIndex值。如果不需要参数,则可以传递一个空字符串。 |
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();
}
}
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.PivotGrid.Web
Imports DevExpress.ExpressApp.Web
Imports DevExpress.ExpressApp.Web.Templates
Imports DevExpress.ExpressApp.Web.Utils
Imports DevExpress.Web.ASPxPivotGrid
' ...
Public Class PivotDrillDownController
Inherits ViewController(Of ListView)
' ...
Private ReadOnly handlerId As String
Protected Overrides Sub OnViewControlsCreated()
MyBase.OnViewControlsCreated()
Dim pivotGridListEditor As ASPxPivotGridListEditor = TryCast(View.Editor, ASPxPivotGridListEditor)
If pivotGridListEditor IsNot Nothing Then
Dim pivotGrid As ASPxPivotGrid = pivotGridListEditor.PivotGridControl
Dim script As String = CallbackManager.GetScript(handlerId, "e.ColumnIndex ';' e.RowIndex")
ClientSideEventsHelper.AssignClientHandlerSafe(pivotGrid, "CellClick", "function(s, e) {" & script & "}", "PivotDrillDownController")
End If
End Sub
Public Sub New()
handlerId = "PivotDrillDownHandler" & GetHashCode()
End Sub
End Class
使用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) {
}
// ...
}
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Web.Templates
' ...
Public Class PivotDrillDownController
Inherits ViewController(Of ListView)
Implements IXafCallbackHandler
'...
Private ReadOnly handlerId As String
Protected Overrides Sub OnViewControlsCreated()
MyBase.OnViewControlsCreated()
CallbackManager.RegisterHandler(handlerId, Me)
' ...
End Sub
Private Sub ProcessAction(ByVal parameter As String) Implements IXafCallbackHandler.ProcessAction
End Sub
End Class
将接收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);
}
}
Imports System
Imports System.Collections
Imports System.Linq
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.PivotGrid.Web
Imports DevExpress.ExpressApp.Web.Templates
Imports DevExpress.XtraPivotGrid
' ...
Private Sub ProcessAction(ByVal parameter As String) Implements IXafCallbackHandler.ProcessAction
Dim indices() As String = parameter.Split(";"c)
Dim columnIndex As Integer = Int32.Parse(indices(0))
Dim rowIndex As Integer = Int32.Parse(indices(1))
Dim drillDown As PivotDrillDownDataSource = (CType(View.Editor, ASPxPivotGridListEditor)).PivotGridControl.CreateDrillDownDataSource(columnIndex, rowIndex)
Dim name As String = View.ObjectTypeInfo.KeyMember.Name
Dim keysToShow As IList = drillDown.Cast(Of PivotDrillDownDataRow)().Where(Function(row) row(name) IsNot Nothing).Select(Function(row) row(name)).ToList()
If keysToShow.Count > 0 Then
Dim targetType As Type = View.ObjectTypeInfo.Type
Dim viewId As String = Application.GetListViewId(targetType)
Dim collectionSource As CollectionSourceBase = Application.CreateCollectionSource(Application.CreateObjectSpace(targetType), targetType, viewId)
collectionSource.Criteria("SelectedObjects") = New InOperator(ObjectSpace.GetKeyPropertyName(targetType), keysToShow)
Dim listView As ListView = Application.CreateListView(viewId, collectionSource, False)
Application.ShowViewStrategy.ShowViewInPopupWindow(listView)
End If
End Sub