vici mvc开发第九篇——Custom Controls(自定义控件)

一、Creating a custom control class

vici mvc框架容许你创建自己的控件,这些控件你可以在任何时候调用,也可以绑定到任何一个窗体,当然这个窗体必须与控件建立一些关系。

为了创建自定义控件,你需要继承抽象的control类并且重写 GenerateHtml(), HandlePostback()和数据库方法。

下面是一个我们写的一个简单的自定义控件:

在自定义控件中下面三个方法是必须的:

1、GenerateHtml()

GenerateHtml()方法在框架呈现控件前调用,这个方法的返回值是XHTML

参数解释:

view(type:view):对将要呈现对应控件的视图的引用,此参数的主要用途是向视图中添加所需的 javascript。

className(type:string)和calssError:(type:string):用于对应控件的CSS class的名称, 为方便呈现 CSS 类名称的 HTML 代码,可以使用 AddClassAttribute 方法的控件的基类。

基类Control拥有 "Id", "Name" 和"Enabled"这些普通属性,你可以调用如下方法添加这些属性:

  • AddIdAttribute()
  • AddNameAttribute()
  • AddEnabledAttribute()
  • AddOnChangeAttribute()

通过上面的例子,你会明白如何调用这些方法。

2、HandlePostback()

参数解释:

postData (type: ClientDataCollection):控件中通过form返回的数据集合

二、Common properties for all controls

PropertyTypeDescription
Id StringThe XHTML id to render the control with. This should be unique across the rendered page. If not set, the framework will generate a unique id for you.
Name StringThe name of the control, which is required and should only be set by the constructor
Error BooleanIf set to true, the control will be renderd with the CSS class defined by the CssClassError property. If false, the CSS class defined by CssClass will be used
CssClass StringThe CSS class name to use when rendering the control (if the Error property is false)
CssClassError StringThe CSS class name to use when rendering the control (if the Error property is true)
AutoPost BooleanSetting this to true will render the attribute οnchange="this.form.submit()"
OnChange StringDefines a line of javascript to execute when the control is changed on the client. The javascript will be prepended to the javascript generated by the AutoPost property
Enabled BooleanSetting this to false causes the control to be renderd with the disabled="disabled" attribute

有一点必须明白,就是框架并不会自动地呈现控件必须的属性,你可以调用 AddIdAttribute(), AddNameAttribute()等。这一点是很重要的,因为所有的属性对于每个控件不一定都是有效的。你也许会想自己处理这些属性等。

三、Using custom controls in form objects

在vici mvc框架中使用你自定义的控件还需要写一个类继承FormFieldAttribute,这个特性告诉vici mvc框架在呈现窗体和处理返回值时调用哪个控件。

注意俩点,第一该类必须继承FormFieldAttribute,第二,必须重写一下几个方法:

1、 CreateControl()

CreateControl()方法会创建一个control,它可能是自定义控件,也可能是[[Projects:Mvc:UserGuide:Controls|built-in controls].你必须设置好所以控件的必须属性,在上面的例子里,我们定义的OnClic在调用属性时可以使用,此属性会传递给控件。

2、GetControlValue()

该方法可以得到控件传递过来的值和返回他,请确保返回的值可转换为相关联的字段的类型。通过调用 FieldType属性可以得到相关联字段的值。

3、SetControlValue()

该方法用来设置对应控件的值,相关联字段的值通过参数传递。

4、IsRightType()

确保你添加到对应字段属性的类型是正确的,如果是错误的,将会抛出异常。

5、Validate()

验证值(参数传递过来的),此方法只应检查值是否有效,该方法没有一个验证的统一规则,你也可以使用窗体的验证机制。


未完待续:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值