protected override void OnInit(EventArgs e) { IBindingTarget page = this.Page as IBindingTarget; if (page == null) { throw new InvalidOperationException("This control can only be used on pages that implement IBindingTarget"); } WebformsControlService controlService = new WebformsControlService(); IBindingTarget ctrl = controlService.FindControlRecursive(page, this.GetType()); if (ctrl != null && ctrl.UniqueID != this.UniqueID) { throw new InvalidOperationException("Only one control of type BindingOptions can appear of each page"); } base.OnInit(e); }
/// <summary> /// Bind using a global Resource binding /// </summary> /// <param name="control"></param> /// <param name="resourceID">The id of the binding which should be used</param> /// <returns></returns> public static object BindResource(this object control, string resourceID) { Control ctrl = control as Control; IControlService service = new WebformsControlService(); IBindingTarget target = new WebformControl(ctrl.Page); WebformControl webFormControl = service.FindControlRecursive(target, typeof(BindingOptionsControl)) as WebformControl; BindingOptionsControl bindingOptions = webFormControl.Control as BindingOptionsControl; if (bindingOptions == null) { throw new InvalidOperationException(NEED_BINDING_OPTIONS_CONTROL); } if (bindingOptions.Resources == null) { throw new InvalidOperationException(NEED_BINDING_OPTIONS_RESOURCES); } Options options = bindingOptions.Resources.Where(r => r.ID == resourceID).FirstOrDefault(); if (options == null) { if (bindingOptions.Resources == null) { throw new InvalidOperationException(NEED_BINDING_RESOURCE); } } if (options.Mode == BindingMode.Command) { BindCommand(control, options); return(null); } else { return(Bind(control, options)); } }