/// <summary> /// Summary description for wwWebDataControlHelper. /// </summary> public class wwWebDataHelper { /// <summary> /// Binds all databound controls on a WebForm page by calling their BindData methods. /// </summary> /// <param name="Container">the parent container (Page)</param> public static void FormBindData(Page WebForm) { FormBindData((Control) WebForm, WebForm); } /// <summary> /// Binds all databound controls on a WebForm page by calling their BindData methods. /// </summary> /// <param name="Container">the control to databind</param> /// <param name="Form">the master container that contains the control (page)</param> static void FormBindData(Control Container, Page WebForm) { // *** Drill through each control on the form foreach( Control loControl in Container.Controls) { // ** Recursively call down into any containers if (loControl.Controls.Count > 0) wwWebDataHelper.FormBindData(loControl, WebForm); // ** only work on those that support interface if (loControl is IwwWebDataControl ) { IwwWebDataControl control = (IwwWebDataControl) loControl; try { //*** Call the BindData method on the control control.GetType().InvokeMember("BindData",System.Reflection.BindingFlags.InvokeMethod, null,control, new object[1] { WebForm } ); } catch(Exception) { // *** Display Error info try { ///TODO: This must be adjusted for specific types of controls control.Text = "** Field binding Error **"; } catch(Exception) {;} } } } } public static BindingError[] FormUnbindData(Page WebForm) { BindingError[] Errors = null; FormUnbindData(WebForm,WebForm,ref Errors); return Errors; } static BindingError[] FormUnbindData(Control Container, Page WebForm, ref BindingError[] Errors) { // *** Drill through each of the controls foreach( Control loControl in Container.Controls) { // ** Recursively call down into containers if (loControl.Controls.Count > 0) FormUnbindData(loControl, WebForm,ref Errors); if (loControl is IwwWebDataControl ) { IwwWebDataControl control = (IwwWebDataControl) loControl; try { control.GetType().InvokeMember("UnbindData",System.Reflection.BindingFlags.InvokeMethod, null,control, new object[1] { WebForm } ); } catch(Exception ex) { // *** Display Error info try { BindingError loError = new BindingError(); if (wwUtils.Empty(control.BindingErrorMessage)) loError.Message = "Invalid data format for " + loControl.ID; else loError.Message = control.BindingErrorMessage; loError.ErrorMsg = ex.Message; loError.Source = ex.Source; loError.StackTrace = ex.StackTrace; loError.ObjectName = loControl.ID; if (Errors == null) { Errors = new BindingError[1]; Errors[0] = loError; } else { // *** Resize the array and assign Error int lnSize = Errors.GetLength(0); Array loTemp = Array.CreateInstance(typeof(BindingError),lnSize + 1); Errors.CopyTo(loTemp,0); loTemp.SetValue(loError,lnSize); Errors = (BindingError[]) loTemp; } } catch(Exception) {;} } } } return Errors; } public static string BindingErrorsToString(BindingError[] Errors) { // *** Optional Error Parsing if (Errors != null) { string lcErrors = ""; for (int x = 0; x < Errors.Length; x++) { lcErrors = lcErrors + Errors[x].Message + "<br>"; } if (!wwUtils.Empty(lcErrors)) return lcErrors; } return ""; } /// <summary> /// /// </summary> /// <param name="WebPage"></param> /// <param name="ActiveControl"></param> /// <param name="ControlSourceObject"></param> /// <param name="ControlSourceProperty"></param> /// <param name="BindingProperty"></param> public static void ControlBindData(Page WebPage, Control ActiveControl, string ControlSourceObject, string ControlSourceProperty, string BindingProperty) { try { if (ControlSourceObject == null || ControlSourceObject.Length == 0 || ControlSourceProperty == null || ControlSourceProperty.Length == 0) return; object loControlSource = wwUtils.GetPropertyEx(WebPage,ControlSourceObject); if (loControlSource == null) return; object loValue; if (loControlSource is System.Data.DataSet) { string lcTable = ControlSourceProperty.Substring(0,ControlSourceProperty.IndexOf(".")); string lcColumn = ControlSourceProperty.Substring(ControlSourceProperty.IndexOf(".")+1); DataSet Ds = (DataSet) loControlSource; loValue = Ds.Tables[lcTable].Rows[0][lcColumn]; } else if(loControlSource is System.Data.DataRow) { DataRow Dr = (DataRow) loControlSource; loValue = Dr[ControlSourceProperty]; } else { loValue = wwUtils.GetPropertyEx(loControlSource,ControlSourceProperty); } /// *** Figure out the type of the control we're binding to object loBindValue = wwUtils.GetProperty(ActiveControl,BindingProperty); string lcControlSourceType = loBindValue.GetType().Name; if (loValue == null) if (lcControlSourceType == "String") wwUtils.SetProperty(ActiveControl,BindingProperty,""); else if (lcControlSourceType == "Boolean") wwUtils.SetProperty(ActiveControl,BindingProperty,false); else wwUtils.SetProperty(ActiveControl,BindingProperty,""); else { ///TODO: Fix Formatting here if (lcControlSourceType == "Boolean") wwUtils.SetProperty(ActiveControl,BindingProperty,loValue); else wwUtils.SetProperty(ActiveControl,BindingProperty,loValue.ToString()); } } catch(Exception) { throw(new Exception("Can't bind " + ActiveControl.ID + " to " + ControlSourceObject + "." + ControlSourceProperty)); } } /// <summary> /// Unbinds control properties back into the control source /// </summary> /// <param name="WebPage">The WebForm Page object</param> /// <param name="ActiveControl">The control to unbind</param> /// <param name="ControlSourceObject"></param> /// <param name="ControlSourceProperty"></param> /// <param name="BindingProperty"></param> public static void ControlUnbindData(Page WebPage, Control ActiveControl, string ControlSourceObject, string ControlSourceProperty, string BindingProperty) { if (ControlSourceObject == null || ControlSourceObject.Length == 0 || ControlSourceProperty == null || ControlSourceProperty.Length == 0) return; object loControlSource = wwUtils.GetPropertyEx(WebPage,ControlSourceObject); if (loControlSource == null) return; // Retrieve the new value from the control object loValue = wwUtils.GetPropertyEx(ActiveControl,BindingProperty); // Try to retrieve the type of the ControlSourceProperty string lcControlSourceType; string lcDataColumn = null; string lcDataTable = null; if (loControlSource is System.Data.DataSet) { lcDataTable = ControlSourceProperty.Substring(0,ControlSourceProperty.IndexOf(".")); lcDataColumn = ControlSourceProperty.Substring(ControlSourceProperty.IndexOf(".")+1); DataSet Ds = (DataSet) loControlSource; lcControlSourceType = Ds.Tables[lcDataTable].Columns[lcDataColumn].DataType.Name; } else if(loControlSource is System.Data.DataRow) { DataRow Dr = (DataRow) loControlSource; lcControlSourceType = Dr.Table.Columns[ControlSourceProperty].DataType.Name; } else { // *** It's an object property MemberInfo[] loInfo = loControlSource.GetType().GetMember(ControlSourceProperty); if (loInfo[0].MemberType.ToString() == "Field") { FieldInfo loField = (FieldInfo) loInfo[0]; lcControlSourceType = loField.FieldType.Name; } else { PropertyInfo loField = (PropertyInfo) loInfo[0]; lcControlSourceType = loField.PropertyType.Name; } } /// Retrieve the value object loAssignedValue; if ( lcControlSourceType == "String") loAssignedValue = loValue; else if (lcControlSourceType == "Int16") loAssignedValue = Convert.ToInt16(loValue); else if (lcControlSourceType == "Int32") loAssignedValue = Convert.ToInt32(loValue); else if (lcControlSourceType == "Int64") loAssignedValue = Convert.ToInt64(loValue); else if (lcControlSourceType == "Byte") loAssignedValue = Convert.ToByte(loValue); else if (lcControlSourceType == "Decimal") loAssignedValue = Convert.ToDecimal(loValue); else if (lcControlSourceType == "Double") loAssignedValue = Convert.ToDouble(loValue); else if (lcControlSourceType == "Boolean") { loAssignedValue = loValue; // lcValue = lcValue.ToLower(); // if (lcValue == "on" || lcValue == "true" || lcValue == "1") // loAssignedValue = true; // else // loAssignedValue = false; } else if (lcControlSourceType == "DateTime") loAssignedValue = Convert.ToDateTime(loValue); else // Not HANDLED!!! throw(new Exception("Field Type not Handled by Data unbinding")); /// Write the value back to the underlying object/data item if (loControlSource is System.Data.DataSet) { DataSet Ds = (DataSet) loControlSource; Ds.Tables[lcDataTable].Rows[0][lcDataColumn] = loAssignedValue; } else if(loControlSource is System.Data.DataRow) { DataRow Dr = (DataRow) loControlSource; Dr[ControlSourceProperty] = loAssignedValue; } else wwUtils.SetPropertyEx(loControlSource,ControlSourceProperty,loAssignedValue); } }Enjoy <g>,