public void ApplyModelTopage_CheckBox()
        {
            TestParticipantClass  testPart = new TestParticipantClass();
            WFObjectValueProvider provider = new WFObjectValueProvider(testPart, "");
            Page     testPage      = new Page();
            CheckBox AcceptedRules = new CheckBox()
            {
                ID = "AcceptedRules"
            };

            testPage.Controls.Add(AcceptedRules);

            WebControlUtilities.ApplyModelToPage(testPage, provider);

            Assert.IsFalse(AcceptedRules.Checked);

            testPart.AcceptedRules = false;
            WebControlUtilities.ApplyModelToPage(testPage, provider);

            Assert.IsFalse(AcceptedRules.Checked);

            testPart.AcceptedRules = true;
            WebControlUtilities.ApplyModelToPage(testPage, provider);

            Assert.IsTrue(AcceptedRules.Checked);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            State.Items.Add(new ListItem()
            {
                Text = "Select State", Value = ""
            });
            foreach (SelectListItem sli in StateUtility.GetStateList(State.SelectedValue ?? ""))
            {
                State.Items.Add(new ListItem()
                {
                    Text = sli.Text, Value = sli.Value
                });
            }

            //If we are loading the page for the first time ...
            if (!Page.IsPostBack)
            {
                //Load a sample customer
                Model = Customer.LoadMockup();
                //This ApplyModelToPage call will create an IWFValueProvider from the model object
                //..and apply the values from this instance to the page controls.
                WebControlUtilities.ApplyModelToPage(this, Model);
                WebControlUtilities.ApplyModelToPage(this, Model.Address);
                WebControlUtilities.ApplyModelToPage(this, Model.Login);
            }
        }
        public void ApplyModelToPage_TextBox()
        {
            TestParticipantClass  testPart = new TestParticipantClass();
            WFObjectValueProvider provider = new WFObjectValueProvider(testPart, "");
            Page    testPage  = new Page();
            TextBox FirstName = new TextBox()
            {
                ID = "FirstName"
            };

            testPage.Controls.Add(FirstName);

            //Apply null value (since it is a property initial state should be null)
            WebControlUtilities.ApplyModelToPage(testPage, provider);

            Assert.AreEqual("", ((TextBox)testPage.FindControl("FirstName")).Text);

            testPart.FirstName = "John";

            WebControlUtilities.ApplyModelToPage(testPage, provider);

            Assert.AreEqual("John", ((TextBox)testPage.FindControl("FirstName")).Text);

            testPart.FirstName = "";

            WebControlUtilities.ApplyModelToPage(testPage, provider);

            Assert.AreEqual("", ((TextBox)testPage.FindControl("FirstName")).Text);
        }
Beispiel #4
0
        public virtual Control GetTargetControl()
        {
            PropertyInfo prop          = GetTargetProperty();
            Control      targetControl = this.FindControl(this.TargetControl); //Search siblings

            if (targetControl == null)                                         //Search page
            {
                targetControl = WebControlUtilities.FindControlRecursive(this.Page, this.TargetControl);
            }
            return(targetControl);
        }
Beispiel #5
0
        /// <summary>
        /// Handles the Click event of the addAlbum control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void addAlbum_Click(object sender, EventArgs e)
        {
            Album album = new Album {
                Name = albumName.Text.Trim(), Description = description.Text.Trim(), UserId = CurrentUser.UserId
            };

            AlbumLogic.Add(album);

            message.Text = string.Format(AdminResources.SuccessfulAlbumAdd, album.Name);

            //Clears the textboxes
            WebControlUtilities.ClearTextFromControl <TextBox>(Controls);
        }
Beispiel #6
0
 /// <summary>
 /// Use WebControlUtilities.GetControlValue() to obtain the value of the control with a matching (server) ID.
 /// This method checks if the control is present in the collection (ContainsKey()) and returns defaultValue if it is not found<br/>
 /// or if the value of the control (when cast to a string) is null or empty.
 /// </summary>
 /// <param name="keyName">The (server) ID of the control to get the value from.</param>
 /// <param name="defaultValue">The value to return if the control is not found.</param>
 /// <returns>The value of the control or defaultValue if not found.</returns>
 public object KeyValue(string keyName, object defaultValue)
 {
     if (ContainsKey(keyName))
     {
         string ctlValue = WebControlUtilities.GetControlValue(_WebControls[keyName]);
         if (String.IsNullOrEmpty(ctlValue))
         {
             return(defaultValue);
         }
         return(ctlValue);
     }
     return(defaultValue);
 }
        public void ApplyModelToPage_MultipleTests()
        {
            TestParticipantClass testPart = new TestParticipantClass();

            testPart.FirstName = "John";
            testPart.LastName  = "Doe";
            testPart.BirthDate = null;

            Page px = new Page();

            px.Controls.Add(new TextBox()
            {
                ID = "FirstName"
            });
            px.Controls.Add(new TextBox()
            {
                ID = "LastName"
            });
            px.Controls.Add(new TextBox()
            {
                ID = "BirthDate"
            });

            WebControlUtilities.ApplyModelToPage(px, testPart);

            TextBox txtFirstName = (TextBox)px.FindControl("FirstName");
            TextBox txtLastName  = (TextBox)px.FindControl("LastName");
            TextBox txtBirthDate = (TextBox)px.FindControl("BirthDate");

            Assert.AreEqual(testPart.FirstName, txtFirstName.Text);
            Assert.AreEqual(testPart.LastName, txtLastName.Text);
            //BirthDate is null, so we expect blank
            Assert.AreEqual("", txtBirthDate.Text);

            txtFirstName.Text = "Jack";
            txtLastName.Text  = "Sparrow";
            txtBirthDate.Text = "1/1/2001";

            TestParticipantClass        part2         = new TestParticipantClass();
            WFPageControlsValueProvider valueProvider = new WFPageControlsValueProvider(px, "");

            WFPageUtilities.UpdateModel(valueProvider, part2, "", null, null);

            Assert.AreEqual("Jack", part2.FirstName);
            Assert.AreEqual("Sparrow", part2.LastName);
            Assert.AreEqual(DateTime.Parse("1/1/2001"), part2.BirthDate);
        }
        public void ApplyModelToPage_DropDownList()
        {
            //Test applying a string property to ListBox
            TestParticipantClass  testPart = new TestParticipantClass();
            WFObjectValueProvider provider = new WFObjectValueProvider(testPart, "");
            Page         testPage          = new Page();
            DropDownList FirstName         = new DropDownList()
            {
                ID = "FirstName"
            };

            testPage.Controls.Add(FirstName);

            //null property value to empty listbox
            WebControlUtilities.ApplyModelToPage(testPage, provider);

            //null property value to listbox with empty item, but other selected
            FirstName.Items.Add(new ListItem("John", "1")
            {
                Selected = true
            });
            FirstName.Items.Add(new ListItem("Mark", "2"));
            FirstName.Items.Add(new ListItem("Joe", "3"));
            FirstName.Items.Add(new ListItem("Select a Name", ""));

            WebControlUtilities.ApplyModelToPage(testPage, provider);

            Assert.AreEqual(3, FirstName.SelectedIndex);

            //specific value

            testPart.FirstName = "2"; //go by value, not by text

            WebControlUtilities.ApplyModelToPage(testPage, provider);

            Assert.AreEqual(1, FirstName.SelectedIndex);

            testPart.FirstName = "NonExistantValue";

            WebControlUtilities.ApplyModelToPage(testPage, provider);

            //different from listbox. This should have the first item in the list selected
            //because a dropdownlist even though set to "-1" cannot have "no item selected"

            Assert.AreEqual(0, FirstName.SelectedIndex);
        }
Beispiel #9
0
        /// <summary>
        /// Handles the Click event of the addGallery control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void addGallery_Click(object sender, EventArgs e)
        {
            Gallery gallery = new Gallery
            {
                Name = galleryName.Text.Trim(),

                UserId      = CurrentUser.UserId,
                Description = description.Text.Trim()
            };

            new GalleryLogic().Insert(gallery);

            //display message
            message.Text = string.Format(AdminResources.SuccessfulGalleryAdd, gallery.Name);

            //Clears the textboxes
            WebControlUtilities.ClearTextFromControl <TextBox>(Controls);
        }
Beispiel #10
0
        public void ApplyModelToPage_IWFControlValue()
        {
            TestParticipantClass  testPart = new TestParticipantClass();
            WFObjectValueProvider provider = new WFObjectValueProvider(testPart, "");
            Page testPage = new Page();
            TestControlValueControl testControl = new TestControlValueControl()
            {
                ID = "FirstName"
            };

            testPage.Controls.Add(testControl);

            WebControlUtilities.ApplyModelToPage(testPage, provider);

            Assert.AreEqual("", testControl.ControlValue);

            testPart.FirstName = "Sam";

            WebControlUtilities.ApplyModelToPage(testPage, provider);

            Assert.AreEqual("Sam", testControl.ControlValue);
        }
Beispiel #11
0
        public void FindValidatorsTest()
        {
            Page p = new Page();
            DataAnnotationValidatorControl testControl = new DataAnnotationValidatorControl()
            {
                ID = "testControl1"
            };
            DataAnnotationValidatorControl testControl2 = new DataAnnotationValidatorControl()
            {
                ID = "testControl2"
            };
            DataAnnotationValidatorControl testControl3 = new DataAnnotationValidatorControl()
            {
                ID = "testControl3"
            };
            DataAnnotationValidatorControl testControl4 = new DataAnnotationValidatorControl()
            {
                ID = "testControl4"
            };

            p.Controls.Add(testControl);
            Panel panel1 = new Panel();
            Panel panel2 = new Panel();

            panel1.Controls.Add(testControl2);
            panel2.Controls.Add(testControl3);

            p.Controls.Add(panel1);
            p.Controls.Add(panel2);

            Assert.IsNotNull(WebControlUtilities.FindControlRecursive(p, "testControl1"));
            Assert.IsNotNull(WebControlUtilities.FindControlRecursive(p, "testControl2"));
            Assert.IsNotNull(WebControlUtilities.FindControlRecursive(p, "testControl3"));

            Assert.IsNull(WebControlUtilities.FindControlRecursive(panel1, "testControl1"));
            Assert.IsNotNull(WebControlUtilities.FindControlRecursive(panel1, "testControl2"));
        }
Beispiel #12
0
        public void FindControlRecursiveTest()
        {
            Page    p           = new Page();
            TextBox testControl = new TextBox()
            {
                ID = "testControl1"
            };
            TextBox testControl2 = new TextBox()
            {
                ID = "testControl2"
            };
            TextBox testControl3 = new TextBox()
            {
                ID = "testControl3"
            };
            TextBox testControl4 = new TextBox()
            {
                ID = "testControl4"
            };

            p.Controls.Add(testControl);
            Panel panel1 = new Panel();
            Panel panel2 = new Panel();

            panel1.Controls.Add(testControl2);
            panel2.Controls.Add(testControl3);

            p.Controls.Add(panel1);
            p.Controls.Add(panel2);

            Assert.IsNotNull(WebControlUtilities.FindControlRecursive(p, "testControl1"));
            Assert.IsNotNull(WebControlUtilities.FindControlRecursive(p, "testControl2"));
            Assert.IsNotNull(WebControlUtilities.FindControlRecursive(p, "testControl3"));

            Assert.IsNull(WebControlUtilities.FindControlRecursive(panel1, "testControl1"));
            Assert.IsNotNull(WebControlUtilities.FindControlRecursive(panel1, "testControl2"));
        }
Beispiel #13
0
        protected override void RenderContents(HtmlTextWriter output)
        {
            WFModelMetaData metadata = new WFModelMetaData();

            foreach (DataAnnotationValidatorControl dvc in WebControlUtilities.FindValidators(this.Page))
            {
                WFModelMetaProperty metaprop = WebControlUtilities.GetMetaPropertyFromValidator(this.Page, dvc, metadata);
                metaprop.OverriddenSpanID = dvc.UniqueID;
                if (!String.IsNullOrEmpty(dvc.Text))
                {
                    metaprop.OverriddenErrorMessage = dvc.Text;
                }
                metadata.Properties.Add(metaprop);
            }
            if (Unobtrusive)
            {
                output.Write(WFScriptGenerator.SetupClientUnobtrusiveValidationScriptHtmlTag().Render());
            }
            else
            {
                output.Write(WFScriptGenerator.SetupClientValidationScriptHtmlTag().Render());
                string targetid = "";
                if (String.IsNullOrEmpty(TargetFormClientID))
                {
                    targetid = this.Page.Form.ClientID;
                }
                else
                {
                    targetid = TargetFormClientID;
                }
                output.Write(new HtmlTag("script", new { type = "text/javascript", language = "javascript" })
                {
                    InnerText = WFScriptGenerator.EnableClientValidationScript(metadata, targetid)
                }.Render());
            }
        }
Beispiel #14
0
 /// <summary>
 /// Provide values against an ASP.net server control (or page)
 /// </summary>
 /// <param name="page">The instance of the page or control on which values will be flattened.</param>
 /// <param name="prefix">An optional prefix to select (server side) control names.</param>
 public WFPageControlsValueProvider(Control page, string prefix)
 {
     _Page        = page;
     _Prefix      = prefix;
     _WebControls = WebControlUtilities.FlattenPageControls(_Page);
 }
Beispiel #15
0
 /// <summary>
 /// Use WebControlUtilities.GetControlValue() to obtain the value of the control with a matching (server) ID.
 /// The collection will not be scanned to verify the control is present.
 /// </summary>
 /// <param name="keyName">The (server) ID of the control to get the value from.</param>
 /// <returns>The value of the control.</returns>
 public object KeyValue(string keyName)
 {
     return(WebControlUtilities.GetControlValue(_WebControls[keyName]));
 }
Beispiel #16
0
        public void ApplyModelToPage_ListBox()
        {
            //Test applying a string property to ListBox
            TestParticipantClass  testPart = new TestParticipantClass();
            WFObjectValueProvider provider = new WFObjectValueProvider(testPart, "");
            Page    testPage  = new Page();
            ListBox FirstName = new ListBox()
            {
                ID = "FirstName"
            };

            testPage.Controls.Add(FirstName);

            //null property value to empty listbox
            WebControlUtilities.ApplyModelToPage(testPage, provider);

            //null property value to listbox with empty item, but other selected
            FirstName.Items.Add(new ListItem("John", "1")
            {
                Selected = true
            });
            FirstName.Items.Add(new ListItem("Mark", "2"));
            FirstName.Items.Add(new ListItem("Joe", "3"));
            FirstName.Items.Add(new ListItem("Select a Name", ""));

            WebControlUtilities.ApplyModelToPage(testPage, provider);

            Assert.AreEqual(3, FirstName.SelectedIndex);

            //specific value

            testPart.FirstName = "2"; //go by value, not by text

            WebControlUtilities.ApplyModelToPage(testPage, provider);

            Assert.AreEqual(1, FirstName.SelectedIndex);

            testPart.FirstName = "NonExistantValue";

            WebControlUtilities.ApplyModelToPage(testPage, provider);

            Assert.AreEqual(-1, FirstName.SelectedIndex);

            //test multiple values

            testPage.Controls.Remove(FirstName);

            testPart.LuckyNumbers = new int[] { 1, 3 };

            ListBox LuckyNumbers = new ListBox();

            LuckyNumbers.Items.Add(new ListItem("1", "1"));
            LuckyNumbers.Items.Add(new ListItem("2", "2"));
            LuckyNumbers.Items.Add(new ListItem("3", "3"));
            LuckyNumbers.Items.Add(new ListItem("4", "4"));
            LuckyNumbers.Items.Add(new ListItem("5", "5"));

            //multiple values while in single mode
            LuckyNumbers.SelectionMode = ListSelectionMode.Single;
            LuckyNumbers.ID            = "LuckyNumbers";
            testPage.Controls.Add(LuckyNumbers);

            WebControlUtilities.ApplyModelToPage(testPage, provider);
            //because we are in single mode, the array shouldn't be enumerated
            //returns "System.Int32[]..."
            Assert.AreEqual(-1, LuckyNumbers.SelectedIndex);

            //now change to multiple mode. Both items in the array should be selected.
            //No other items should be selected.
            LuckyNumbers.SelectionMode = ListSelectionMode.Multiple;

            WebControlUtilities.ApplyModelToPage(testPage, provider);

            foreach (ListItem li in LuckyNumbers.Items)
            {
                if (!li.Selected && testPart.LuckyNumbers.Contains(int.Parse(li.Value)))
                {
                    Assert.Fail("Item " + li.Value + " should be selected.");
                }
                else if (li.Selected && !testPart.LuckyNumbers.Contains(int.Parse(li.Value)))
                {
                    Assert.Fail("Item " + li.Value + " should not be selected.");
                }
            }
        }
Beispiel #17
0
        public void ApplyModelToPage_ListBoxDropDownAdditionalTests()
        {
            ListBox lbItems = new ListBox()
            {
                ID = "Inventory"
            };

            lbItems.Items.Add(new ListItem()
            {
                Text = "Item1", Value = "1"
            });
            lbItems.Items.Add(new ListItem()
            {
                Text = "Item2", Value = "2"
            });
            lbItems.Items.Add(new ListItem()
            {
                Text = "Item3", Value = "3"
            });
            lbItems.Items.Add(new ListItem()
            {
                Text = "Item4", Value = "4"
            });

            DropDownList ddlItems = new DropDownList()
            {
                ID = "LuckyPhrases"
            };

            ddlItems.Items.Add(new ListItem()
            {
                Text = "Phrase1", Value = "A lucky man"
            });
            ddlItems.Items.Add(new ListItem()
            {
                Text = "Phrase2", Value = "Good fortune"
            });
            ddlItems.Items.Add(new ListItem()
            {
                Text = "Phrase3", Value = "Rich and wealthy"
            });
            ddlItems.Items.Add(new ListItem()
            {
                Text = "Phrase4", Value = "Excellent health"
            });

            ListBox lbGroups = new ListBox()
            {
                ID = "ParticipantGroupID"
            };

            lbGroups.Items.Add(new ListItem()
            {
                Text = "Admin", Value = "1"
            });
            lbGroups.Items.Add(new ListItem()
            {
                Text = "Judge", Value = "2"
            });
            lbGroups.Items.Add(new ListItem()
            {
                Text = "User", Value = "3"
            });
            lbGroups.Items.Add(new ListItem()
            {
                Text = "Outsider", Value = "4"
            });

            Page px = new Page();

            //px.Controls.Add(lbItems);
            //px.Controls.Add(ddlItems);
            px.Controls.Add(lbGroups);

            lbGroups.Items[2].Selected = true;

            //Pull selected values into participant
            TestParticipantClass        testPart      = new TestParticipantClass();
            WFPageControlsValueProvider valueProvider = new WFPageControlsValueProvider(px, "");

            WFPageUtilities.UpdateModel(valueProvider, testPart, "", null, null);

            Assert.AreEqual(3, testPart.ParticipantGroupID);


            //Apply null values to null collections
            WebControlUtilities.ApplyModelToPage(px, testPart);

            Assert.IsNull(testPart.LuckyPhrases, null);
            Assert.IsNull(testPart.Inventory, null);

            //Apply null values to non-null collections
            testPart.LuckyPhrases = new string[] { "A lucky man", "Rich and wealthy" };
            testPart.Inventory    = new List <TestInventoryObject>();
            WebControlUtilities.ApplyModelToPage(px, testPart);

            Assert.IsNotNull(testPart.Inventory);
            Assert.AreEqual(0, testPart.Inventory.Count);
            Assert.AreEqual(2, testPart.LuckyPhrases.Length);

            //Apply select one value
            lbItems.Items[2].Selected  = true;
            ddlItems.Items[2].Selected = true;

            WebControlUtilities.ApplyModelToPage(px, testPart);
        }
Beispiel #18
0
        protected override bool EvaluateIsValid()
        {
            _HasBeenChecked = true;
            if (String.IsNullOrEmpty(SourceTypeString))
            {
                if (SourceType == null &&
                    String.IsNullOrEmpty(XmlRuleSetName) &&
                    this.Page as IWFGetValidationRulesForPage == null)
                {
                    throw new Exception("The SourceType and SourceTypeString properties are null/empty on one of the validator controls.\r\nPopulate either property.\r\nie: control.SourceType = typeof(Widget); OR in markup SourceTypeString=\"Assembly.Classes.Widget, Assembly\"\r\nFinally, the page can also implement IWFGetValidationRulesForPage.");
                }
                else if (SourceType == null &&
                         !String.IsNullOrEmpty(XmlRuleSetName))
                {
                    //Get the type from the XmlRuleSet
                    SourceType = WFUtilities.GetRuleSetForName(XmlRuleSetName).ModelType;
                }
                else if (SourceType == null && String.IsNullOrEmpty(XmlRuleSetName))
                {
                    SourceType = ((IWFGetValidationRulesForPage)this.Page).GetValidationClassType();
                }
            }
            else
            {
                try {
                    SourceType = Type.GetType(SourceTypeString, true, true);
                } catch (Exception ex) {
                    throw new Exception("Couldn't resolve type " + SourceTypeString + ". You may need to specify the fully qualified assembly name.");
                }
            }

            PropertyInfo prop = WFUtilities.GetTargetProperty(_propertyName, SourceType);

            Control validateControl = this.FindControl(this.ControlToValidate); //Search siblings

            if (validateControl == null)                                        //Search page
            {
                validateControl = WebControlUtilities.FindControlRecursive(this.Page, this.ControlToValidate);
            }
            string controlValue = WebControlUtilities.GetControlValue(validateControl);

            if (String.IsNullOrEmpty(XmlRuleSetName))
            {
                var    displayNameAttr = prop.GetCustomAttributes(typeof(DisplayNameAttribute), true).OfType <DisplayNameAttribute>().FirstOrDefault();
                string displayName     = displayNameAttr == null ? prop.Name : displayNameAttr.DisplayName;

                foreach (var attr in prop.GetCustomAttributes(typeof(ValidationAttribute), true).OfType <ValidationAttribute>())
                {
                    if (attr as IWFRequireValueProviderContext != null)
                    {
                        ((IWFRequireValueProviderContext)attr).SetValueProvider(new WFPageControlsValueProvider(this.Page, ""));
                    }

                    if (!attr.IsValid(controlValue))
                    {
                        this.ErrorMessage = attr.FormatErrorMessage(displayName);
                        return(false);
                    }
                }
                return(true);
            }
            else
            {
                XmlDataAnnotationsRuleSet         ruleset  = WFUtilities.GetRuleSetForType(SourceType, XmlRuleSetName);
                XmlDataAnnotationsRuleSetProperty property = ruleset.Properties.FirstOrDefault(p => p.PropertyName == PropertyName);
                if (property != null)
                {
                    foreach (XmlDataAnnotationsValidator val in property.Validators)
                    {
                        ValidationAttribute attr = WFUtilities.GetValidatorInstanceForXmlDataAnnotationsValidator(val);

                        if (attr as IWFRequireValueProviderContext != null)
                        {
                            ((IWFRequireValueProviderContext)attr).SetValueProvider(new WFPageControlsValueProvider(this.Page, ""));
                        }

                        if (!String.IsNullOrEmpty(ErrorMessage) &&
                            String.IsNullOrEmpty(attr.ErrorMessage))
                        {
                            attr.ErrorMessage = ErrorMessage;
                        }

                        foreach (var key in val.ValidatorAttributes.Keys)
                        {
                            PropertyInfo pi = attr.GetType().GetProperty(key);

                            string[] excludeProps = new string[] { };
                            if (attr.GetType() == typeof(StringLengthAttribute))
                            {
                                excludeProps = new string[] { "maximumLength" };
                            }
                            if (attr.GetType() == typeof(RangeAttribute))
                            {
                                excludeProps = new string[] { "minimum", "maximum" };
                            }
                            if (attr.GetType() == typeof(RegularExpressionAttribute))
                            {
                                excludeProps = new string[] { "pattern" };
                            }
                            if (!excludeProps.Contains(key))
                            {
                                pi.SetValue(attr, Convert.ChangeType(val.ValidatorAttributes[key], pi.PropertyType), null);
                            }
                        }
                        if (!attr.IsValid(controlValue))
                        {
                            this.ErrorMessage = attr.FormatErrorMessage(property.DisplayName ?? "");
                            return(false);
                        }
                    }
                }

                return(true);
            }
        }
        /// <summary>
        /// Handles the Click event of the saveButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void saveButton_Click(object sender, EventArgs e)
        {
            User user = new User
            {
                Access    = 0,
                Email     = emailAddressTextBox.Text.Trim(),
                FirstName = firstNameTextBox.Text.Trim(),
                LastName  = lastNameTextBox.Text.Trim(),
                Website   = websiteTextBox.Text.Trim()
            };

            bool passwordFieldsHaveValues = !string.IsNullOrEmpty(passwordOneTextBox.Text.Trim()) && !string.IsNullOrEmpty(passwordTwoTextBox.Text.Trim());
            bool isValidPassword          = true;

            //Check passwords if they exist
            if (passwordFieldsHaveValues)
            {
                isValidPassword = IsValidPassword();

                if (isValidPassword)
                {
                    user.Password = SimpleHash.ComputeHash(passwordOneTextBox.Text.Trim(), SimpleHash.Algorithm.SHA256, new byte[8]);
                }
                else
                {
                    message.Text = "Passwords don't match";
                }
            }
            else
            {
                if (!IsUserEdit)
                {
                    message.Text = "New Users require a password";
                }
            }


            //Check that there is a valid password and the error text is empty
            if (isValidPassword && string.IsNullOrEmpty(message.Text))
            {
                if (IsUserEdit)
                {
                    //If no password values were entered we need to keep the old password.
                    if (!passwordFieldsHaveValues)
                    {
                        user.Password = editUser.Password;
                    }

                    //Set user Id
                    user.UserId = userId;

                    //update the user and display the success message.
                    new UserLogic().Update(user);
                    //message.Text = string.Format(AdminResources.SuccessfulUserUpdate);
                    Response.Redirect("~/Admin/Manage.aspx?a=Users", false);
                }
                else
                {
                    new UserLogic().Add(user);
                    message.Text = string.Format(AdminResources.SuccessfulUserAdd, user.Email);
                }

                //Clears the textboxes
                WebControlUtilities.ClearTextFromControl <TextBox>(Controls);
            }
        }
Beispiel #20
0
        protected override void RenderContents(HtmlTextWriter output)
        {
            HtmlTag tblGrid = new HtmlTag("table");

            tblGrid.Attr("cellspacing", "0");
            tblGrid.Attr("rules", "all");
            tblGrid.Attr("border", "1");
            tblGrid.Attr("id", this.ClientID);
            tblGrid.Attr("style", "border-collapse:collapse;");

            HtmlTag trHeader = new HtmlTag("tr");

            WebControlUtilities.ApplyTableItemStyleToTag(trHeader, HeaderStyle);

            IEnumerable ds = DataSource as IEnumerable;

            if (ds == null)
            {
                ds = (DataSource as IListSource).GetList() as IEnumerable;
            }
            //asp.net's datagrid requires all objects be the same type as the first object
            Type dataType = null;

            PropertyInfo[]        properties = null;
            IEnumerator           ie         = ds.GetEnumerator();
            List <GridControlRow> rowDef     = new List <GridControlRow>();

            //Get property data from the first element
            if (ie.MoveNext())
            {
                dataType   = ie.Current.GetType();
                properties = dataType.GetProperties();

                HtmlTag firstRow = new HtmlTag("tr");

                properties = properties ?? ie.Current.GetType().GetProperties();
                //Create rows
                foreach (PropertyInfo pi in properties)
                {
                    rowDef.Add(new GridControlRow()
                    {
                        ColumnName = pi.Name
                    });
                    HtmlTag tdHdr = new HtmlTag("td")
                    {
                        InnerText = pi.Name
                    };
                    trHeader.Children.Add(tdHdr);
                    firstRow.Children.Add(new HtmlTag("td")
                    {
                        InnerText = pi.GetValue(ie.Current, null).ToString()
                    });
                }

                tblGrid.Children.Add(trHeader);
                tblGrid.Children.Add(firstRow);

                //Get the rest
                while (ie.MoveNext())
                {
                    HtmlTag currentRow = new HtmlTag("tr");
                    foreach (PropertyInfo pi in properties)
                    {
                        currentRow.Children.Add(new HtmlTag("td")
                        {
                            InnerText = pi.GetValue(ie.Current, null).ToString()
                        });
                    }
                    tblGrid.Children.Add(currentRow);
                }
            }

            tblGrid.MergeObjectProperties(this.Attributes);

            output.Write(tblGrid.Render());
        }