using System; using System.Collections.Generic; using Composite.C1Console.Forms; using Composite.Plugins.Forms.WebChannel.UiControlFactories; public partial class Controls_MyControl : UserControlBasedUiControl, IValidatingUiControl { #region Dummy Data - this could easily be dynamically loaded from data layer public Dictionary PersonSource = new Dictionary() { {Guid.Empty, "select a person ..."}, {Guid.Parse("9bdb7b6d-4e4f-41c9-bcc3-95b20bd8d791"), "Person 1"}, {Guid.Parse("9bdb7b6d-4e4f-41c9-bcc3-95b20bd8d792"), "Person 2"}, {Guid.Parse("9bdb7b6d-4e4f-41c9-bcc3-95b20bd8d793"), "Person 3"}, {Guid.Parse("9bdb7b6d-4e4f-41c9-bcc3-95b20bd8d794"), "Person 4"}, {Guid.Parse("9bdb7b6d-4e4f-41c9-bcc3-95b20bd8d795"), "Person 5"} }; public Dictionary OrganizationSource = new Dictionary() { {Guid.Empty, "select an organization ..."}, {Guid.Parse("4bdb7b6d-4e4f-41c9-bcc3-95b20bd8d791"), "Organization 1"}, {Guid.Parse("4bdb7b6d-4e4f-41c9-bcc3-95b20bd8d792"), "Organization 2"}, {Guid.Parse("4bdb7b6d-4e4f-41c9-bcc3-95b20bd8d793"), "Organization 3"}, {Guid.Parse("4bdb7b6d-4e4f-41c9-bcc3-95b20bd8d794"), "Organization 4"}, {Guid.Parse("4bdb7b6d-4e4f-41c9-bcc3-95b20bd8d795"), "Organization 5"} }; #endregion #region Form Properties [FormsProperty()] [BindableProperty()] public Nullable PersonGuid { get; set; } [FormsProperty()] [BindableProperty()] public Nullable OrganizationGuid { get; set; } #endregion protected void Page_Load(object sender, EventArgs e) { // attach an event to the typeSelector typeSelector.SelectedIndexChanged += new EventHandler(typeSelector_SelectedIndexChanged); } #region UserControlBasedUiControl // this is called when the control is initialized - we should grab values from our 'BindableProperty' fields above and show it on our ASP.NET controls. public override void BindStateToControlProperties() { Guid selectedValue = Guid.Parse(itemSelector.SelectedValue); if (selectedValue != Guid.Empty) { switch (typeSelector.SelectedValue) { case "Person": this.PersonGuid = selectedValue; this.OrganizationGuid = null; break; case "Organization": this.OrganizationGuid = selectedValue; this.PersonGuid = null; break; } } else { this.PersonGuid = this.OrganizationGuid = null; } } public override void InitializeViewState() { typeSelector.SelectedValue = this.PersonGuid != null ? "Person" : "Organization"; FillDropDown(); itemSelector.SelectedValue = this.PersonGuid != null ? this.PersonGuid.ToString() : this.OrganizationGuid.ToString(); } #endregion #region IValidatingUiControl // If any selections are invalid, we should return false here. We get called for each field we bind to. public bool IsValid { get { return (this.PersonGuid != null || this.OrganizationGuid != null); } } public string ValidationError { get { return "Missing value.\n"; } } #endregion #region Private methods void typeSelector_SelectedIndexChanged(object sender, EventArgs e) { FillDropDown(); } private void FillDropDown() { switch (typeSelector.SelectedValue) { case "Person": itemSelector.DataSource = PersonSource; itemSelector.DataBind(); break; case "Organization": itemSelector.DataSource = OrganizationSource; itemSelector.DataBind(); break; } } #endregion }