Example #1
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.Validate();

            int index = Convert.ToInt32(ViewState["EditedIndex"]);
            int id    = Convert.ToInt32(ViewState["EditedId"]);

            RepeaterItem   item = repUsers.Items[index];
            MyUserDataGrid my   = new MyUserDataGrid();

            if (id != -1)
            {
                my = rep.GetEntityById(id) as MyUserDataGrid;
            }

            for (int i = 0; i < item.Controls.Count; i++)
            {
                TextBox t = item.Controls[i] as TextBox;
                if (t != null)
                {
                    string propName = t.ToolTip;

                    switch (propName)
                    {
                    case "Email":
                        my.Email = t.Text;
                        break;

                    case "Password":
                        if (my.Password != t.Text)
                        {
                            my.Password = t.Text;
                        }
                        break;

                    case "Birthday":
                        DateTime birth = new DateTime();
                        DateTime.TryParse(t.Text, out birth);
                        if (my.Birthday != birth && birth != new DateTime())
                        {
                            my.Birthday = birth;
                        }
                        break;

                    case "Name":
                        if (my.Name != t.Text)
                        {
                            my.Name = t.Text;
                        }
                        break;

                    case "Surname":
                        if (my.Surname != t.Text)
                        {
                            my.Surname = t.Text;
                        }
                        break;

                    case "FathersName":
                        if (my.FathersName != t.Text)
                        {
                            my.FathersName = t.Text;
                        }
                        break;

                    case "Phone":
                        if (my.Phone != t.Text)
                        {
                            my.Phone = t.Text;
                        }
                        break;

                    default:
                        break;
                    }

                    t.Style.Add("display", "none");
                }
                else
                {
                    DropDownList lst = item.Controls[i] as DropDownList;

                    if (lst != null)
                    {
                        if (my.City != lst.SelectedValue)
                        {
                            my.City = lst.SelectedValue;
                        }

                        lst.Style.Add("display", "none");
                    }
                }
            }

            rep.Save(my);

            if (id == -1)
            {
                id = rep.GetAll().Max(u => u.Id);
                UserRepositoryBase b = rep as UserRepositoryBase;

                b.AddRole(id, "Admin");
            }

            ViewState.Add("EditedId", -1);
            ViewState.Add("EditedIndex", -1);

            btnSave.Style.Add("display", "none");
            btnCancel.Style.Add("display", "none");

            isEditing = false;

            UpdateData();
        }