예제 #1
0
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "alpha" || e.CommandName == "NoFilter")
        {
            String value = null;
            switch (e.CommandName)
            {
            case ("alpha"):
            {
                value = string.Format("{0}%", e.CommandArgument);
                break;
            }

            case ("NoFilter"):
            {
                value = "%";
                break;
            }
            }
            ObjectDataSource1.SelectParameters["UserName"].DefaultValue = value;
            ObjectDataSource1.DataBind();
            RadGrid1.Rebind();
        }
        else if (e.CommandName == "QuickUpdate")
        {
            string UserName, Role;
            var    oUser = new User();

            foreach (GridDataItem item in RadGrid1.Items)
            {
                UserName = item["UserName"].Text;
                Role     = ((RadComboBox)item.FindControl("ddlRole")).SelectedValue;
                oUser.ChangeRole(UserName, Role);
            }
        }
        else if (e.CommandName == "PerformInsert" || e.CommandName == "Update")
        {
            try
            {
                var    command         = e.CommandName;
                var    row             = command == "PerformInsert" ? (GridEditFormInsertItem)e.Item : (GridEditFormItem)e.Item;
                var    FileAvatarImage = (RadUpload)row.FindControl("FileAvatarImage");
                string strAvatarImage  = FileAvatarImage.UploadedFiles.Count > 0 ? FileAvatarImage.UploadedFiles[0].FileName : "";
                var    strUserID       = ((HiddenField)row.FindControl("hdnUserID")).Value;
                //var oUser = new User();

                var AddressBookID = ((HiddenField)row.FindControl("hdnAddressBookID")).Value.ToString().Trim();
                var hdnUserName   = ((HiddenField)row.FindControl("hdnUserName")).Value.ToString().Trim();
                var hdnEmail      = ((HiddenField)row.FindControl("hdnEmail")).Value.ToString().Trim();
                var strUserName   = ((TextBox)row.FindControl("txtUserName")).Text;
                if (strUserName == "")
                {
                    strUserName = hdnUserName;
                }
                string ConvertedFirstName = Common.ConvertTitle(strUserName);
                var    strEmail           = ((TextBox)row.FindControl("txtEmail")).Text;
                if (strEmail == "")
                {
                    strEmail = hdnEmail;
                }
                var    strPassword     = ((TextBox)row.FindControl("txtPassword")).Text;
                var    strCompanyName  = ((TextBox)row.FindControl("txtCompanyName")).Text;
                var    strAddress1     = ((TextBox)row.FindControl("txtAddress")).Text;
                var    strFullName     = ((TextBox)row.FindControl("txtFullName")).Text;
                var    strCellPhone    = ((TextBox)row.FindControl("txtCellPhone")).Text;
                string strOldImageName = ((HiddenField)row.FindControl("hdnOldImageName")).Value;

                if (e.CommandName == "PerformInsert")
                {
                    var      ListViewRoles = (ListView)row.FindControl("lvListViewRoles");
                    string[] dsRoles       = Roles.GetRolesForUser(strUserName);

                    Membership.CreateUser(strUserName, strPassword, strEmail);

                    if (string.IsNullOrEmpty(strUserID))
                    {
                        strUserID = Membership.GetUser(strUserName).ProviderUserKey.ToString();
                    }

                    foreach (ListViewItem item in ListViewRoles.Items)
                    {
                        var check = (CheckBox)item.FindControl("chkRoles");
                        if (check.Checked == true)
                        {
                            Roles.AddUserToRole(strUserName, check.Text);
                        }
                    }
                    var oUserProfile = new AddressBook();
                    oUserProfile.AddressBookInsert2(
                        strFullName,
                        "",
                        strEmail,
                        strCellPhone,
                        "",
                        "",
                        strEmail,//strUserName,
                        strCompanyName,
                        strAddress1,
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",
                        "member",
                        "",
                        ""
                        );
                    //var oUserProfile = new UserProfile();
                    //oUserProfile.UserProfileInsert(strUserID, strCompanyName, strFullName, "", "", strCellPhone, "", "", "true");
                }
                else
                {
                    var      UserName      = ((HiddenField)row.FindControl("hdnUserName")).Value.ToString().Trim();
                    var      ListViewRoles = (ListView)row.FindControl("lvListViewRoles");
                    string[] dsRoles       = Roles.GetRolesForUser(UserName);

                    Roles.RemoveUserFromRoles(UserName, dsRoles);

                    foreach (ListViewItem item in ListViewRoles.Items)
                    {
                        var check = (CheckBox)item.FindControl("chkRoles");
                        if (check.Checked == true)
                        {
                            Roles.AddUserToRole(UserName, check.Text);
                        }
                    }

                    var oUserProfile = new AddressBook();
                    oUserProfile.AddressBookUpdate2(
                        AddressBookID,
                        strFullName,
                        "",
                        strEmail,
                        strCellPhone,
                        "",
                        "",
                        strEmail,//strUserName,
                        strCompanyName,
                        strAddress1,
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",
                        "",
                        ""
                        );

                    //var oUserProfile = new UserProfile();
                    //oUserProfile.UserProfileUpdate(
                    //    strUserID,
                    //    strCompanyName,
                    //    strFullName,
                    //    "",
                    //    "",
                    //    strCellPhone,
                    //    "",
                    //    strAvatarImage,
                    //    "true"
                    //    );
                }
                var strOldImagePath = Server.MapPath("~/res/userprofile/" + strOldImageName);

                if (!string.IsNullOrEmpty(strAvatarImage))
                {
                    if (File.Exists(strOldImagePath))
                    {
                        File.Delete(strOldImagePath);
                    }
                    string strFullPath = "~/res/userprofile/" + (string.IsNullOrEmpty(ConvertedFirstName) ? "" : ConvertedFirstName) + strAvatarImage.Substring(strAvatarImage.LastIndexOf('.'));

                    FileAvatarImage.UploadedFiles[0].SaveAs(Server.MapPath(strFullPath));
                    ResizeCropImage.ResizeByCondition(strFullPath, 100, 100);
                }
                RadGrid1.Rebind();
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        else if (e.CommandName == "DeleteSelected")
        {
            string OldAvatarImage;
            var    oUserProfile = new AddressBook();
            var    oUser        = new User();

            string errorList = "", UserName = "";

            foreach (GridDataItem item in RadGrid1.SelectedItems)
            {
                try
                {
                    var UserID = item.GetDataKeyValue("UserID").ToString();
                    UserName = item["UserName"].Text;
                    oUserProfile.AddressBookDeleteByUserName(UserName);
                    oUser.UserDelete(UserName);

                    OldAvatarImage = ((HiddenField)item.FindControl("hdnAvatarImage")).Value;
                    DeleteImage(OldAvatarImage);
                }
                catch (Exception ex)
                {
                    lblError.Text = ex.Message;
                    if (ex.Message == ((int)ErrorNumber.ConstraintConflicted).ToString())
                    {
                        errorList += ", " + UserName;
                    }
                }
            }
            if (!string.IsNullOrEmpty(errorList))
            {
                e.Canceled = true;
                string strAlertMessage = "Tài khoản <b>\"" + errorList.Remove(0, 1).Trim() + " \"</b> đang có tin đăng BĐS .<br /> Xin xóa tin đăng hoặc sử dụng chức năng khoá tài khoản.";
                lblError.Text = strAlertMessage;
            }
            RadGrid1.Rebind();
        }
    }