Esempio n. 1
0
        private TextBox SetupTextboxTest(Options options)
        {
            /*Arrange*/
            TextBox tb1 = new TextBox();

            tb1.Text = "Data From view";
            tb1.ID   = "tb1";
            WebformControl wrappedTb = new WebformControl(tb1);

            //Create a binding collection, add a binding
            options.Path = "EmployeeName";
            collection   = new Binding.BindingCollection()
            {
                new Binding.BindingDef(wrappedTb, "Text", options, false, controlService)
            };

            //and setup service to return it
            controlService.Expect(cs => cs.Unwrap(null)).Do(new Func <IBindingTarget, object>((t) => tb1));

            //setup the data storage service to return the collection
            dataStorageService.Expect(ds => ds.RetrieveOrCreate <Binding.BindingCollection>(DATA_BINDING_KEY)).Return(collection);
            dataStorageService.Expect(ds => ds.RetrieveOrCreate <Binding.BindingCollection>(COMMAND_BINDING_KEY)).Return(new Binding.BindingCollection());

            //and the viewmodel (we are using the view state binder which will try to resolve the service from storage
            dataStorageService.Expect(ds => ds.Retrieve <object>(null)).IgnoreArguments().Return(viewModel);

            return(tb1);
        }
Esempio n. 2
0
        private static bool TryGetBindingOptionsControl(IBindingContainer bindingContainer, out BindingOptionsControl bindingOptionsControl)
        {
            bool result = false;

            bindingOptionsControl = null;

            Page page = bindingContainer as Page;

            if (page == null)
            {
                throw new InvalidOperationException("This method binding extension can only be used within the context of an asp.net page");
            }

            IBindingTarget  target                = new WebformControl(page);
            IControlService controlService        = GetControlService(bindingContainer);
            WebformControl  wrappedOptionsControl = controlService.FindControlRecursive(target, typeof(BindingOptionsControl)) as WebformControl;

            if (wrappedOptionsControl != null)
            {
                bindingOptionsControl = wrappedOptionsControl.Control as BindingOptionsControl;

                if (bindingOptionsControl != null)
                {
                    result = true;
                }
            }

            return(result);
        }
Esempio n. 3
0
        private Tuple <TextBox, TextBox> SetupMultipleTextboxTest(Options options1, Options options2)
        {
            /*Arrange*/
            TextBox tb1 = new TextBox();

            tb1.Text = "Data From tb1";
            tb1.ID   = "tb1";
            WebformControl wrappedTb = new WebformControl(tb1);

            TextBox tb2 = new TextBox();

            tb2.Text = "Data From tb2";
            tb2.ID   = "tb2";
            WebformControl wrappedTb2 = new WebformControl(tb2);

            //Create a binding collection, add a binding
            options1.Path = "EmployeeName";
            collection    = new Binding.BindingCollection()
            {
                new Binding.BindingDef(wrappedTb, "Text", options1, false, controlService)
            };

            options2.Path = "EmployeeName";
            collection.Add(new Binding.BindingDef(wrappedTb2, "Text", options2, false, controlService));


            //and setup service to return it
            int count = 0;

            controlService.Expect(cs => cs.Unwrap(null)).Do(new Func <IBindingTarget, object>((t) =>
            {
                if (count == 0)
                {
                    count++;
                    return(tb1);
                }
                else
                {
                    return(tb2);
                }
            }));

            //setup the data storage service to return the collection
            dataStorageService.Expect(ds => ds.RetrieveOrCreate <Binding.BindingCollection>(DATA_BINDING_KEY)).Return(collection);
            dataStorageService.Expect(ds => ds.RetrieveOrCreate <Binding.BindingCollection>(COMMAND_BINDING_KEY)).Return(new Binding.BindingCollection());

            //and the viewmodel (we are using the view state binder which will try to resolve the service from storage
            dataStorageService.Expect(ds => ds.Retrieve <object>(null)).IgnoreArguments().Return(viewModel);

            return(new Tuple <TextBox, TextBox> {
                Item1 = tb1, Item2 = tb2
            });
        }
        private Control Resolve(IBindingTarget iTarget)
        {
            Control target = iTarget as Control;

            if (target == null)
            {
                WebformControl webformControl = iTarget as WebformControl;
                if (webformControl != null)
                {
                    target = webformControl.Control;
                }
            }

            return(target);
        }
        public IBindingTarget FindControlRecursive(IBindingTarget root, string id)
        {
            if (root.ID == id)
            {
                return(root);
            }

            foreach (Control ctl in this.GetChildren(root))
            {
                IBindingTarget webFormControl = new WebformControl(ctl);
                IBindingTarget foundCtl       = FindControlRecursive(webFormControl, id);
                if (foundCtl != null)
                {
                    return(foundCtl);
                }
            }

            return(null);
        }
        public IBindingTarget FindControlUnique(IBindingTarget root, string unique)
        {
            if (root.UniqueID == unique)
            {
                return(root);
            }

            foreach (Control ctl in this.GetChildren(root))
            {
                IBindingTarget webFormControl = new WebformControl(ctl);
                IBindingTarget foundCtl       = FindControlUnique(webFormControl, unique);
                if (foundCtl != null)
                {
                    return(foundCtl);
                }
            }

            return(null);
        }
Esempio n. 7
0
        /// <summary>
        /// Bind and specify options for the bind.
        /// </summary>
        /// <param name="control"></param>
        /// <param name="bindingOptions"></param>
        /// <returns></returns>
        public static object Bind(object control, Options bindingOptions, string propertyName)
        {
            Control        ctrl           = control as Control;
            WebformControl wrappedControl = new WebformControl(ctrl);

            IBindingContainer bindingContainer = ctrl.Page as IBindingContainer;
            BinderBase        binder           = CreateBinder(bindingContainer);

            object bindingResult = null;

            if (ctrl != null && bindingContainer != null)
            {
                IDataItemContainer dataItemContainer = ctrl.NamingContainer as IDataItemContainer;

                if (dataItemContainer != null)
                {
                    WebformControl <IDataItemContainer> wrappedContainer = new WebformControl <IDataItemContainer>((Control)dataItemContainer);

                    if (string.IsNullOrEmpty(propertyName))
                    {
                        bindingResult = binder.ExecuteBind(wrappedControl, wrappedContainer, bindingOptions);
                    }
                    else
                    {
                        bindingResult = binder.ExecuteBind(wrappedControl, wrappedContainer, bindingOptions, propertyName);
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(propertyName))
                    {
                        bindingResult = binder.ExecuteBind(wrappedControl, bindingOptions);
                    }
                    else
                    {
                        bindingResult = binder.ExecuteBind(wrappedControl, bindingOptions, propertyName);
                    }
                }
            }

            return(bindingResult);
        }
        public object Unwrap(IBindingTarget bindingTarget)
        {
            if (bindingTarget == null)
            {
                throw new ArgumentException("Cannot unwrap null object", "bindingTarget");
            }

            WebformControl webControl = bindingTarget as WebformControl;

            if (webControl == null)
            {
                throw new ArgumentException(string.Format("Unable to unwrap objects of type {0}", bindingTarget.GetType().FullName), "bindingTarget");
            }

            if (webControl.Control == null)
            {
                throw new ArgumentException("Unwrapped object is null", "bindingTarget");
            }

            return(webControl.Control);
        }
        public IBindingTarget FindControlRecursive(IBindingTarget root, Type type)
        {
            Control resolvedRoot = Resolve(root);

            if (resolvedRoot.GetType() == type)
            {
                return(root);
            }

            foreach (Control ctl in this.GetChildren(root))
            {
                IBindingTarget webFormControl = new WebformControl(ctl);
                IBindingTarget foundCtl       = FindControlRecursive(webFormControl, type);
                if (foundCtl != null)
                {
                    return(foundCtl);
                }
            }

            return(null);
        }
Esempio n. 10
0
        /// <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));
            }
        }
Esempio n. 11
0
        private List <Control> SetupCollectionUnbindTest(Options options)
        {
            List <Control> createdControls = new List <Control>();

            //Create a repeater from which we will read data
            Repeater rpt = new Repeater();

            rpt.ItemTemplate = new DynamicItemTemplate(new List <Func <Control> > {
                () => new TextBox()
                {
                    ID = "tb"
                }
            });
            var item1 = new Child {
                Name = "Sam"
            };
            var item2 = new Child {
                Name = "Ben"
            };
            var item3 = new Child {
                Name = "Ali"
            };
            var list = new List <Child> {
                item1, item2, item3
            };

            viewModel.ChildrensNames = list;

            //Create a binding collection
            collection = new Binding.BindingCollection();

            //add in the parent binding, this will not be used in the bind but is required for unbind to work within a collection
            collection.Add(new Binding.BindingDef(new WebformControl(rpt), "DataSource", new Options {
                Path = "ChildrensNames"
            }, true, controlService));

            //When the item is created, create a binding for it and add to the binding collection.
            //We will also had the itemtemplate create control to a collection so that we can stick some values
            //in to simulate user input before unbinding to the model.
            int index = 0;

            rpt.ItemDataBound += (s, e) =>
            {
                TextBox        tb        = e.Item.FindControl("tb") as TextBox;
                WebformControl wrappedTb = new WebformControl(tb);
                options.Path = "Name";
                Binding.BindingDef binding = new Binding.BindingDef(wrappedTb, "Text", index, collection, options, false, controlService);
                index++;
                collection.Add(binding);
                createdControls.Add(tb);
            };

            rpt.DataSource = list;


            WebformControl dummyControl = new WebformControl(new TextBox());

            //tell the mock conntrol service to return the collection result when required.
            this.controlService.Expect(cs => cs.FindControlUnique(null, null)).IgnoreArguments()
            .Do(
                new Func <IBindingTarget, string, IBindingTarget>((b, s) =>
            {
                //we only need it return a non-null
                return(dummyControl);
            }));


            int counter = 0;

            //and setup service to return it
            controlService.Expect(cs => cs.Unwrap(null)).IgnoreArguments().Do(new Func <IBindingTarget, object>((t) =>
            {
                Control ctrl = createdControls[counter];
                counter++;
                return(ctrl);
            }));


            //setup viewmodel with default values
            viewModel.ChildrensNames = new List <Child> {
                new Child(), new Child(), new Child()
            };

            //setup the data storage service to return the collection
            dataStorageService.Expect(ds => ds.RetrieveOrCreate <Binding.BindingCollection>(DATA_BINDING_KEY)).Return(collection);
            dataStorageService.Expect(ds => ds.RetrieveOrCreate <Binding.BindingCollection>(COMMAND_BINDING_KEY)).Return(new Binding.BindingCollection());

            //and the viewmodel (we are using the view state binder which will try to resolve the service from storage
            dataStorageService.Expect(ds => ds.Retrieve <object>(null)).IgnoreArguments().Return(viewModel);

            rpt.DataBind();

            return(createdControls);
        }