using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Reflection;
using WebProjectBLL;
using System.Text;
namespace WebProject.Controls
{
public
class EntityControl :
BaseControl
{
private
BaseEntity boundEntity;
public
virtual BaseEntity BoundEntity
{
get{
return this.boundEntity; }
set{
this.boundEntity = value; }
}
public
virtual void BindControlToEntity()
{
if(
this.boundEntity != null )
{
this.LoadControlsFromIDBEntity( this.boundEntity
);
}
}
public
virtual void BindEntityToControl()
{
if(
this.boundEntity != null )
{
this.LoadIDBEntityFromControls( this.boundEntity
);
}
}
private
void LoadIDBEntityFromControls(
BaseEntity entity )
{
PropertyInfo [] piArray = entity.GetType().GetProperties();
foreach( PropertyInfo prop in piArray )
{
Object [] param = new Object[1];
if(
prop.Name != "IsValid"
&& prop.Name != "ErrorMessage"
)
{
Type propType
= prop.GetGetMethod().ReturnType;
Control controlToBind
= this.FindControl( prop.Name );
if( controlToBind !=
null )
{
if( controlToBind is eWorld.UI.NumericBox )
{
eWorld.UI.NumericBox nb = (eWorld.UI.NumericBox)controlToBind;
if( nb.DecimalPlaces
== -1 ) //it's an int
{
param[ 0 ] =
Convert.ToInt32( nb.Text );
}
else //it's a double
{
param[ 0 ] = Convert.ToDouble( nb.Text );
}
}
else if( controlToBind is TextBox )
{
TextBox
tb = (TextBox)controlToBind;
if( tb.Text.Length
> 0 )
{
param[ 0 ] = tb.Text;
}
}
else if( controlToBind is DropDownList )
{
DropDownList
ddl = (DropDownList)controlToBind;
int selectedIndex = ddl.SelectedIndex;
if( prop.PropertyType.ToString()
== "System.Int32" )
{
param[ 0 ] =
Convert.ToInt32( ddl.SelectedItem.Value );
}
else if( prop.PropertyType.ToString() == "System.Int64" )
{
param[ 0 ] =
Convert.ToInt64( ddl.SelectedItem.Value );
}
else if( prop.PropertyType.ToString() == "System.String"
)
{
param[ 0 ] = ddl.SelectedItem.Value;
}
else
{
throw new
Exception( prop.PropertyType + " Is not a
handled type" );
}
}
else if( controlToBind is eWorld.UI.CalendarPopup )
{
eWorld.UI.CalendarPopup cal = ( eWorld.UI.CalendarPopup)controlToBind;
param[ 0 ] = cal.SelectedDate;
}
else if( controlToBind is CheckBox )
{
CheckBox
cb = (CheckBox)controlToBind;
param[ 0 ] = cb.Checked;
}
else
{
throw new
Exception( "Can't be bound to this control" );
}
prop.SetValue( entity, param[ 0 ], null );
}
}
}
}
private
void LoadControlsFromIDBEntity(
BaseEntity entity )
{
foreach(
Control control in this.Controls )
{
LoadControl( control, entity );
}
}
private
void LoadControl(
Control control, BaseEntity
entity )
{
PropertyInfo prop = null;
if(
control.ID != null )
{
prop = entity.GetType().GetProperty( control.ID );
}
if(
control is eWorld.UI.CollapsablePanel
)
{
foreach(
Control cont in control.Controls
)
{
LoadControl( cont, entity );
}
return;
}
if(
prop != null )
{
Object propValue = prop.GetValue(
entity, null );
if(
control is TextBox )
{
TextBox tb = (TextBox)control;
tb.Text = Convert.ToString(
propValue );
}
else
if( control is DropDownList )
{
DropDownList ddl = (DropDownList)control;
if( propValue != null )
{
ddl.SelectedIndex
= ddl.Items.IndexOf( ddl.Items.FindByValue( propValue.ToString() ) );
}
}
else
if( control is eWorld.UI.CalendarPopup )
{
eWorld.UI.CalendarPopup
cal = ( eWorld.UI.CalendarPopup)control;
cal.SelectedDate
= Convert.ToDateTime( propValue );
}
else
if( control is CheckBox )
{
CheckBox cb = (CheckBox)control;
cb.Checked = Convert.ToBoolean( propValue );
}
else
if( control is eWorld.UI.NumericBox )
{
eWorld.UI.NumericBox
nb = (eWorld.UI.NumericBox)control;
nb.Text = propValue.ToString();
}
else
if( control is
Label )
{
Label label =
(Label)control;
label.Text = propValue.ToString();
}
else
{
}
}
}
protected
virtual void ClearForm()
{
foreach( System.Web.UI.Control c in this.Controls )
{
if(
c is System.Web.UI.WebControls.TextBox
)
{
TextBox tb = (TextBox)c;
tb.Text =
"";
}
if(
c is eWorld.UI.CalendarPopup
)
{
eWorld.UI.CalendarPopup
cp = (eWorld.UI.CalendarPopup)c;
cp.Text =
"";
}
if(
c is System.Web.UI.WebControls.CheckBox
)
{
CheckBox cb = (CheckBox)c;
cb.Checked = false;
}
if(
c is System.Web.UI.WebControls.DropDownList
)
{
DropDownList ddl = (DropDownList)c;
ddl.SelectedIndex
= 0;
}
}
}
protected
virtual void DisableAllControlsOnForm()
{
foreach( System.Web.UI.Control c in this.Controls )
{
if(
c is System.Web.UI.WebControls.WebControl
)
{
System.Web.UI.WebControls.WebControl
wc = (System.Web.UI.WebControls.WebControl)c;
wc.Enabled = false;
}
}
}
protected
virtual long SaveBoundEntity()
{
this.Page.Validate();
if(
this.Page.IsValid )
{
return
BrokerFactory.Instance.Save( this.boundEntity );
}
return
-1;
}
}//class
}//namespace