Page 171 - DCAP312_WEB_TECHNOLOGIES_II
P. 171

Unit 8: Creating More Advanced ASP.NET



                     foreach (DataBindingRegister dbR in                                          Notes
                     this.dataBindingCollection)
                     {
                     Control control = dbR.ControlObject;
                     if (control.ID != null) {
                     if (this.Direction ==
                     BindingDirection.FromUIToBusinessObject)
                     {
                     control.DataBind();
                     }
                     else if (bindingDirection ==
                     BindingDirection.FromBusinessObjectToUI)
                     {
                     PropertyInfo propertyInfo = control.GetType().
                     GetProperty(dbR.ControlObjectPropertyName,
                     BINDING_FLAGS);
                     object obj = propertyInfo.GetValue(control, null);
                     propertyInfo = this.BusinessEntity.GetType().
                     GetProperty(dbR.BusinessObjectPropertyName,
                     BINDING_FLAGS);
                     propertyInfo.SetValue(
                     this.BusinessEntity, obj, null);
                     }
                     }
                     }
                     return this.BusinessEntity;
                     }
            The preceding example calls the Bind() method twice: once in the Page_Load event and once in
            the Button Click event. The Binding Direction enum will hold one of two values: either From
            Business Object To UI or From UI To Business Object.
            That completes the application. Upon execution, it displays the default data bound to the
            controls in the Page_Load event. And if you enter new values in the text boxes and then click
            on the Button control, the form displays the name and address entered in the text boxes after a
            postback. In both cases, the data binding takes place using the Bind() method, which executes
            once in each direction.

            Data binding in ASP.NET 1.x was unidirectional, i.e., the control could in no way update the
            underlying data source to which it was bound. While ASP.NET 2.0 introduced two-way data
            binding, not all the ASP.NET controls supported i; you need to write custom data binding logic
            such as that discussed in this topic to make two-way data binding available for all the controls.




                      Create a two way binding application.




                                             LOVELY PROFESSIONAL UNIVERSITY                                   165
   166   167   168   169   170   171   172   173   174   175   176