开发业务 Web 窗体的创新架构 (3) - 配置 GridView
开发业务 Web 窗体的创新架构 (3) - 配置 GridView
引言
这是本系列第三篇文章,介绍一种创新的架构,用于开发企业软件中的 Web 窗体,与直接编写 ASPX/MVC 代码相比,它具有更高的性能、生产力、可配置性和可维护性。本文介绍了如何在 Web 窗体中配置 gridview
以显示搜索结果。在继续之前,强烈建议您先阅读本系列之前的文章,如下目录所示。
在上篇文章介绍“查询”时,我们已经了解到,由 IDynamicPage
实例返回的 QueryResult
会被 RapidWebDev
UI 框架自动绑定到 GridView
。本文将介绍 GridView
配置的模式。让我们看一个示例如下:
<GridViewPanel HeaderText="Concrete Data Query Results"
EntityName="Concrete Data Record"
EnabledCheckBoxField="true"
PageSize="25"
PrimaryKeyFieldName="Id"
DefaultSortField="LastUpdatedDate"
DefaultSortDirection="DESC">
<ViewButton />
<EditButton />
<DeleteButton />
<Fields>
<Field FieldName="Name" HeaderText="Name" />
<Field FieldName="Value" HeaderText="Value" />
<Field FieldName="DeleteStatus" HeaderText="Status" Width="60">
<Transform-Switch>
<Case Value="NotDeleted"><span style='color:green'>
Enabled</span></Case>
<Case Value="Deleted"><span style='color:red'>
Disabled</span></Case>
</Transform-Switch>
</Field>
<Field FieldName="LastUpdatedBy" HeaderText="ModifiedBy" Align="Center">
<Transform-Callback Type="XXX"/>
</Field>
<Field FieldName="LastUpdatedDate" HeaderText="ModifiedOn"
Align="Center" Width="150" />
<RowView FieldName="Description" />
</Fields>
</GridViewPanel>
属性
HeaderText
:GridView
部分的标题文本。EntityName
: 显示在gridview
中的实体名称,如下屏幕截图所示。PrimaryKeyFieldName
: 绑定实体的[主键]字段。gridview
只支持单个唯一键。PageSize
: Grid view 的页面大小。默认值为 25。EnabledCheckBoxField
: 是否在gridview
的每一行渲染一个checkbox
列。ExecuteQueryWhenLoaded
: 网页加载时是否执行查询。EnabledColumnMove
: 是否允许通过拖放来重新排序列。EnabledColumnResize
: 是否关闭 Grid 的列大小调整功能。DefaultSortField
: 默认排序字段名,应在内部 Field 元素中配置。DefaultSortDirection
: 可选值包括ASC
|DESC
。
* 提示: HeaderText
和 EntityName
支持格式为 $VariableName$
的清单变量,其中 VariableName
应作为键放入代码隐藏的 IApplicationContext.TempVariables
中。它还支持全局化标识符,格式为“$Namespace.ClassName.PropertyName, AssemblyName$
”,无论 static
属性是 internal
、protected
还是 private
。
行查看、编辑和删除按钮
ViewButton
: 如果配置了ViewButton
元素,则在每一行显示查看按钮。该按钮可以配置为图片或按钮。EditButton
: 如果配置了EditButton
元素,则在每一行显示编辑按钮。该按钮可以配置为图片或按钮。DeleteButton
: 如果配置了DeleteButton
元素,则在每一行显示删除按钮。该按钮可以配置为图片或按钮。
或者,当您在 XML 中配置了 ViewButton
、EditButton
和 DeleteButton
时,您还可以通过代码隐藏中的数据来控制它们在每一行中的可见性。请参见内部 IDynamicPage
。
/// <summary>
/// The interface restraints the dynamic page.
/// </summary>
interface IDynamicPage : IDynamicComponent
{
/// <summary>
/// The method will be invoked when the data item binding to grid rows.
/// </summary>
/// <param name="e"></param>
void OnGridRowControlsBind(GridRowControlBindEventArgs e);
}
/// <summary>
/// Event argument to manage visibility of controls in gridview rows of dynamic page.
/// </summary>
class GridRowControlBindEventArgs : EventArgs
{
/// <summary>
/// Gets binding data item which used for callback to set control visibility by data.
/// </summary>
object DataItem { get; private set; }
/// <summary>
/// Sets/gets true to display checkbox column. Default value is true.
/// </summary>
bool ShowCheckBoxColumn { get; set; }
/// <summary>
/// Sets/gets true to display view button in buttons column. Default value is true.
/// </summary>
bool ShowViewButton { get; set; }
/// <summary>
/// Sets/gets true to display edit button in buttons column. Default value is true.
/// </summary>
bool ShowEditButton { get; set; }
/// <summary>
/// Sets/gets true to display delete button in buttons column. Default value is true.
/// </summary>
bool ShowDeleteButton { get; set; }
}
GridView 字段
您可以在 Fields
中配置一个通用的 Field
或 RowView
。
Field 模式
<Attributes>
FieldName
: 绑定的实体字段。它也可以是任何属性访问器,例如Category.Name
、"DicionaryKey"、Properties
"Name" 等。HeaderText
: 列标题文本。SortingFieldName
: 排序字段名。如果未指定值,则默认使用FieldName
进行排序。显式排序字段用于需要按与显示不同的字段进行排序的要求。Sortable
: 列是否可排序Resizable
: 列是否可调整大小Css
: 列中所有单元格的自定义 CSSWidth
: 列的初始宽度(像素),默认为自动。Align
: 列内的文本对齐方式Hidden
: 是否初始隐藏列。用户可以在客户端显示隐藏的列。
HeaderText
支持格式为 $VariableName$
的清单变量,其中 VariableName
应作为键放入代码隐藏的 IApplicationContext.TempVariables
中。它还支持全局化标识符,格式为“$Namespace.ClassName.PropertyName, AssemblyName$
”,无论 static
属性是 internal
、protected
还是 private
。
<Transform>
您可以为字段配置数据转换,用于转换源实体的显示值。RapidWebDev v1.51 支持 4 种转换器。
Transform-ToString-Parameter
: 配置一个格式参数将字段值转换为string
。例如,DateTime.Now.ToString(string format)
Transform-VariableString
: 配置自定义输出,与运行时数据集成。例如,您需要显示一个链接,如<a href="/article.aspx?id=XXX">Text</a>
。您可以将元素值配置为<a href="/article.aspx?id=$Id$">$Text$</a>
。$Marker$
可以在框架渲染到 UI 之前被替换。Marker
应该是绑定实体的可访问属性、IApplicationContext.TempVariables
中的变量或全局化变量标记。Transform-Callback
: 使用接口IGridViewFieldValueTransformCallback
的回调实现来在运行时处理字段值。/// <summary> /// Format input field value. /// </summary> /// <param name="fieldName"></param> /// <param name="fieldValue"></param> /// <returns></returns> string Format(string fieldName, object fieldValue);
Transform-Switch
: 类似于 C# 语言中的switch
...case
... 语法。如果字段值与Case
匹配,则呈现内部文本片段。如果没有Case
匹配,则直接输出字段值。以下 XML 片段旨在将 HTML 标签包装在字段值周围。<Transform-Switch> <Case Value="Enabled" CaseSensitive="true"> <span style="color:green">Enabled</span></Case> <Case Value="Disabled" CaseSensitive="true"> <span style="color:red">Disabled</span></Case> </Transform-Switch>
行视图
行视图用于渲染与实体相关的任何内容,甚至是自定义 HTML 片段。当您在 .NET 3.5 SP1 中开发时,您可以从 IDynamicPage
实现的 Query 方法返回匿名对象,该匿名对象有一个属性,用于组装自定义内容以供行视图使用。
FieldName
: 在 Grid 的行视图中显示的字段。Css
: 在渲染过程中将自定义 CSS 类应用于行视图。TagName
: 包裹字段值的 HTML 标签。默认标签名称为“p
”。