WebControl.cs example
//------------------FOR DEMO USE ONLY----------------------------------
//-------- Copyright by Artem Saveliev artem@savelev.com --------------
//---------------------------------------------------------------------
public static void setStyles(WebControls.IWebControl o,IControl c){
if (c.ControlStyle.width != null) o.Width = c.ControlStyle.width.WebUnit();
if (c.ControlStyle.height != null) o.Height = c.ControlStyle.height.WebUnit();
if (c.ControlStyle.fontSize != null) o.Font.Size = new FontUnit(c.ControlStyle.fontSize.WebUnit());
if (c.ControlStyle.fontFamily != null) o.Font.Names = new string[] {c.ControlStyle.fontFamily};
if (!c.ControlStyle.bgcolor.IsEmpty) o.BackColor = c.ControlStyle.bgcolor;
if (!c.ControlStyle.color.IsEmpty) o.ForeColor = c.ControlStyle.color;
foreach (System.Collections.DictionaryEntry v in c.ControlStyle.unparsed)
o.Style.Add(v.Key.ToString(),v.Value.ToString());
}
private object getClassValue(object o,IControl c){
string[] fieldPath;
System.Type t;
object o2 = o;
try{
if (o!=null && c.DataField != null){ //-- If we are regenerating from postback there's no object at this step
fieldPath = c.DataField.Split('.');
for (int i=0;i<fieldPath.Length;i++){
t = o2.GetType();
if (i<fieldPath.Length - 1)
o2 = t.InvokeMember(fieldPath[i],BindingFlags.Instance|BindingFlags.Public|BindingFlags.GetProperty|BindingFlags.GetField,null,
o2, new object [] {});
else
return t.InvokeMember(fieldPath[i],BindingFlags.Instance|BindingFlags.Public|BindingFlags.GetProperty|BindingFlags.GetField,null,
o2, new object [] {});
}
return null;
} else
return null;
}
catch (Exception e){
ArgumentException ne = new ArgumentException("Field name "+c.DataField+" not found in class "+c.DataClass+". Check your form description and business class");
ne.Source = e.Source;
ne.HelpLink = XFDLFile;
throw(ne);
}
}
private void setClassValue(object o, IControl c, string fieldValue){
string[] fieldPath;
System.Type t;
object o2 = o;
fieldPath = c.DataField.Split('.');
for (int i=0;i<fieldPath.Length;i++){
t = o2.GetType();
try
{
if (i<fieldPath.Length - 1)
o2 = t.InvokeMember(fieldPath[i],BindingFlags.Instance|BindingFlags.Public|BindingFlags.GetProperty|BindingFlags.GetField,null,
o2, new object [] {});
else
t.InvokeMember(fieldPath[i],BindingFlags.Instance|BindingFlags.Public|BindingFlags.SetField|BindingFlags.SetProperty,null,
o2, new object [] {DBTypes.ParseText(fieldValue,c.DataType)});
}
catch (Exception e)
{
throw new Exception("Unable to write property a "+o2.GetType().ToString()+" class. The field name was "+c.DataField+", the calculated property was "+fieldPath[i],e);
}
}
}
public void HandleButton(object sender, System.EventArgs e) {
string buttonName = ((System.Web.UI.WebControls.Button)sender).ID;
tires.Components.XFDL.Controls.Button btn = (tires.Components.XFDL.Controls.Button)doc.FindControl(buttonName);
object o = doc.FindObject(btn.DataClass);
System.Type t = o.GetType();
if (btn.Argument == null)
t.InvokeMember(btn.Method,BindingFlags.Instance|BindingFlags.Public|BindingFlags.InvokeMethod,null,
o, new object [] {});
else
t.InvokeMember(btn.Method,BindingFlags.Instance|BindingFlags.Public|BindingFlags.InvokeMethod,null,
o, new object [] {btn.Argument});
}
public void HandleValueChange(object sender, System.EventArgs e) {
string fieldName = ((System.Web.UI.Control)sender).ID;
string fieldValue = ((WebControls.IWebControl)sender).GetValue();
tires.Components.XFDL.Controls.IControl box = (tires.Components.XFDL.Controls.IControl)doc.FindControl(fieldName);
object o = doc.FindObject(box.DataClass);
setClassValue(o,box,fieldValue);
}
public void AddHandler(string id,System.EventHandler h) {
((System.Web.UI.WebControls.Button)this.FindControl(id)).Click += h;
}
private BaseValidator CreateValidator(IControl c,string controlName)
{
BaseValidator val;
switch (c.DataType)
{
case DataType.Integer:
RangeValidator intv = new RangeValidator();
intv.ControlToValidate = controlName;
intv.ErrorMessage="Numeric Field";
intv.Type = ValidationDataType.Integer;
intv.MaximumValue = Int32.MaxValue.ToString();
intv.MinimumValue = "0";
val = intv;
break;
case DataType.Decimal:
RangeValidator floatv = new RangeValidator();
floatv.ControlToValidate = controlName;
floatv.ErrorMessage="Numeric Field";
floatv.Type = ValidationDataType.Double;
floatv.MaximumValue = Decimal.MaxValue.ToString();
floatv.MinimumValue = Decimal.MinValue.ToString();
val = floatv;
break;
case DataType.Money:
RangeValidator moneyv = new RangeValidator();
moneyv.ControlToValidate = controlName;
moneyv.ErrorMessage="Money Field";
moneyv.Type = ValidationDataType.Currency;
moneyv.MaximumValue = Decimal.MaxValue.ToString();
moneyv.MinimumValue = Decimal.MinValue.ToString();
val = moneyv;
break;
default:
val = null;
break;
}
if (val != null) val.ErrorMessage = " * " + val.ErrorMessage;
return val;
}
public static Control buildControl(IControl c){
switch (c.ControlType) {
case ControlType.Button:
WebControls.Button buttoncontrol = new WebControls.Button();
buttoncontrol.Text = c.Title;
buttoncontrol.ID = c.ControlID;
buttoncontrol.CssClass = "akb";
buttoncontrol.ToolTip = c.Tooltip;
if (c.ControlStyle.width != null) buttoncontrol.Width = c.ControlStyle.width.WebUnit();
if (c.ControlStyle.height != null) buttoncontrol.Height = c.ControlStyle.height.WebUnit();
if (c.ControlStyle.fontSize != null) buttoncontrol.Font.Size = new FontUnit(c.ControlStyle.fontSize.WebUnit());
if (c.ControlStyle.fontFamily != null) buttoncontrol.Font.Names = new string[] {c.ControlStyle.fontFamily};
if (!c.ControlStyle.bgcolor.IsEmpty) buttoncontrol.BackColor = c.ControlStyle.bgcolor;
if (!c.ControlStyle.color.IsEmpty) buttoncontrol.ForeColor = c.ControlStyle.color;
if (c.AccessKey != null) buttoncontrol.AccessKey = c.AccessKey;
foreach (System.Collections.DictionaryEntry v in c.ControlStyle.unparsed)
buttoncontrol.Style.Add(v.Key.ToString(),v.Value.ToString());
return buttoncontrol;
case ControlType.Grid:
EditGridControl gridcontrol = new EditGridControl();
gridcontrol.EnableViewState = true;
gridcontrol.grid = (Controls.Grid)c;
gridcontrol.ID = c.ControlID;
return gridcontrol;
case ControlType.Label:
WebControls.Label ret = new WebControls.Label();
ret.ID = c.ControlID;
ret.ToolTip = c.Tooltip;
ret.EnableViewState = true;
if (c.DataType == tires.Components.XFDL.Controls.DataType.Money) ret.Style.Add("float","right");
setStyles(ret,c);
return ret;
case ControlType.Entry:
switch (c.EntryType) {
case EntryType.List:
WebControls.ListBox lc = new WebControls.ListBox();
lc.ID = c.ControlID;
lc.Attributes.Add("title",c.Tooltip);
setStyles(lc,c);
lc.EnableViewState = false;
if (((Controls.ListBox)c).Rows != 0)
lc.Rows = ((Controls.ListBox)c).Rows;
else
lc.Rows = 1;
if((c.Modifier & tires.Components.XFDL.Controls.Modifier.Identifier) != 0 ||
(c.Modifier & tires.Components.XFDL.Controls.Modifier.ReadOnly) != 0) // set ID parameter
lc.ReadOnly = true;
return lc;
case EntryType.Checkbox:
WebControls.CheckBox cb = new WebControls.CheckBox();
cb.ID = c.ControlID;
cb.ToolTip = c.Tooltip;
cb.EnableViewState = true;
if((c.Modifier & tires.Components.XFDL.Controls.Modifier.Identifier) != 0 ||
(c.Modifier & tires.Components.XFDL.Controls.Modifier.ReadOnly) != 0) // set ID parameter
cb.ReadOnly = true;
return cb;
case EntryType.Date:
case EntryType.Text:
if((c.Modifier & tires.Components.XFDL.Controls.Modifier.Hidden) != 0){
WebControls.Hidden hiddencontrol = new WebControls.Hidden();
hiddencontrol.EnableViewState = false;
hiddencontrol.ID = c.ControlID;
return hiddencontrol;
} else {
WebControls.WebTextBox txtcontrol = new WebControls.WebTextBox();
txtcontrol.ID = c.ControlID;
txtcontrol.ToolTip = c.Tooltip;
setStyles(txtcontrol,c);
if (c.MaxLength > 0) txtcontrol.MaxLength = c.MaxLength;
txtcontrol.Columns = ((tires.Components.XFDL.Controls.TextBox)c).Columns;
if (((tires.Components.XFDL.Controls.TextBox)c).Rows != 0){
txtcontrol.TextMode = TextBoxMode.MultiLine;
txtcontrol.Rows = ((tires.Components.XFDL.Controls.TextBox)c).Rows;
}
if((c.Modifier & tires.Components.XFDL.Controls.Modifier.Identifier) != 0 ||
(c.Modifier & tires.Components.XFDL.Controls.Modifier.ReadOnly) != 0) // set ID parameter
txtcontrol.ReadOnly = true;
if (c.AccessKey != null) txtcontrol.AccessKey = c.AccessKey;
// TODO: move DataType related visual adjustments to separate method
if (c.DataType == tires.Components.XFDL.Controls.DataType.Money) txtcontrol.Style.Add("text-align","right");
txtcontrol.EnableViewState = true;
return txtcontrol;
}
default:
return null;
}
default:
return null;
}
}