Page 170 - DCAP312_WEB_TECHNOLOGIES_II
P. 170
Web Technologies-II
Notes <asp:Button ID= “Button1” runat= “server”
OnClick= “Button1_Click” Text= “Button” />
8.4.2 Using the Data Binding Framework
To use this data binding framework you create an array of DataBindingRegister instances and
populate them with the necessary data binding information in the Page_Load event of the web
form. This array is then added to an instance of the DataBindingRegisterCollection class that
holds a collection of DataBindingRegister instances:
void Page_Load(object sender, EventArgs e) {
DataBindingRegister [] dataBindingRegister = new
DataBindingRegister[2];
// You would normally store the following binding information
// in an XML file
dataBindingRegister[0] = new DataBindingRegister();
dataBindingRegister[0].ControlObject = this.txtEmpName;
dataBindingRegister[0].ControlObjectPropertyName = “Text”;
dataBindingRegister[0].BusinessObject= “employee”;
dataBindingRegister[0].BusinessObjectPropertyName = “EmpName”;
dataBindingRegister[1] = new DataBindingRegister();
dataBindingRegister[1].ControlObject = this.txtEmpAddress;
dataBindingRegister[1].ControlObjectPropertyName = “Text”;
dataBindingRegister[1].BusinessObject = “employee”;
dataBindingRegister[1].BusinessObjectPropertyName =
“EmpAddress”;
//Now, add the data binding information to the
// Data Binding Collection Register
this.bindingManager.DataBindings.Add(dataBindingRegister);
if (!IsPostBack)
{
employeeDO = ReadData();
this.bindingManager.BusinessEntity = employeeDO;
this.bindingManager.Direction =
BindingDirection.FromUIToBusinessObject;
this.bindingManager.Bind(this);
}
}
After storing the data binding information you can bind data from the controls in the user's
interface to the corresponding properties of the business object and vice-versa using the
BindingManager.Bind() method. This method accepts an instance of the Page class as a parameter,
iterates through the collection of databound control instances and then binds or un-binds
data—depending on the binding direction set earlier using the BindingDirection enumeration.
The Bind() method returns an IBusinessEntity instance.
IBusinessEntity IBindingManager.Bind(Page form)
{
164 LOVELY PROFESSIONAL UNIVERSITY