How to set initial values for properties in Ext.Net
How can I set the initial values for custom (or even non custom) controls properties in Ext.Net (an .net wrapper for extjs)?
Currently I'm doing the following:
public class CpfField : Ext.Net.TextField {
public CpfField() {
this.SelectOnFocus = true;
this.AllowBlank = false;
this.MaxLength = 14;
this.FieldLabel = "CPF";
this.LabelAlign = Ext.Net.LabelAlign.Top;
this.Plugins.Add(new CpfInputMask());
}
}
As you can see, I'm using the constructor just to set the default values, I'm not overriding any behavior of the control. So far, so g开发者_如何转开发ood. It works as expected, but I have this.LabelAlign = Ext.Net.LabelAlign.Top
set on each control I inherited.
This smells like violating the DRY
principle. Is there a way to set this (and other properties) in global scope?
What you have here is fine, although I did notice a couple issues.
- The .LabelAlign property must be set at the Container level. The Container must use a FormLayout as well. Unfortunately the .LabelAlign cannot be rendered differently at the Field level.
- Setting the .FieldLabel property to "CPF" shouldn't really be required, unless you anticipate all these "CpfField" components being labeled as "CPF". The .FieldLabel is generally set at the Field configuration level, either in markup or code-behind when the field is instantiated.
Another "global" option you could investigate is using a .skin file. The example below demonstrates this option by "globally" setting a property of all TextField components.
The following sample demonstrates several options including a setting the properties in the OnInit event of the object.
Example (.skin)
<%@ Register assembly="Ext.Net" namespace="Ext.Net" tagprefix="ext" %>
<ext:TextField runat="server" Icon="Accept" />
Example (.aspx)
<%@ Page Language="C#" Theme="Skin1" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
var form = new FormPanel
{
Height = 215,
Width = 350,
Title = "Example",
Padding = 5,
DefaultAnchor = "100%",
Items = {
new MyField
{
FieldLabel = "My Field"
},
new AnotherField
{
FieldLabel = "Another Field"
},
new TextField
{
FieldLabel = "A TextField"
}
}
};
this.Form.Controls.Add(form);
}
public class MyField : TextField
{
public MyField()
{
this.SelectOnFocus = true;
this.AllowBlank = false;
this.MaxLength = 14;
}
}
public class AnotherField : TextField
{
protected override void OnInit(EventArgs e)
{
this.SelectOnFocus = true;
this.AllowBlank = false;
this.MaxLength = 14;
base.OnInit(e);
}
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Ext.NET Example</title>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />
</form>
</body>
</html>
Hope this helps.
精彩评论