コード例 #1
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            WebClient client = new WebClient();

            client.Headers["Content-type"] = "application/json";
            client.Encoding = Encoding.UTF8;
            if (Request.QueryString["status"] == "update")
            {
                ViewState["UserID"] = Request.QueryString["mId"];

                string apiUrlUser = Utilities.BASE_URL + "/api/Users";
                if (Session["SystemRole"].ToString() == "1")
                {
                    Committee.Models.User memberManger = new Models.User()
                    {
                        ID                  = Convert.ToInt32(Request.QueryString["mId"]),
                        Name                = txtMemberName.Text,
                        UserEmailId         = txtMemberEmail.Text,
                        Phone               = txtPhoneNumber.Text,
                        Title               = txtMemberJob.Text,
                        SecrtaryOfDept      = ddlmangerForDept?.SelectedItem?.Value,
                        WorkSide            = txtWorkSide.Text,
                        SystemRole          = Convert.ToInt32(ddlMemberType?.SelectedItem?.Value),
                        UserName            = txtuserNameOfManager.Text,
                        UserPassword        = Encryptor.MD5Hash(txtPasswordOfManager.Text),
                        Address             = txtAddress.Text,
                        UserImage           = "/" + ImgUpload?.PostedFile?.FileName,
                        ManagerOfDepartment = ddlmangerForDept?.SelectedItem?.Value
                    };

                    if (string.IsNullOrEmpty(txtuserNameOfManager.Text))
                    {
                        memberManger.UserName = memberManger.Name;
                    }
                    else
                    {
                        memberManger.UserName = txtuserNameOfManager.Text;
                    }
                    if (ViewState["Phone"].ToString() == txtPhoneNumber.Text.Trim().ToLower())
                    {
                        memberManger.Phone = txtPhoneNumber.Text.Trim().ToLower();
                        if (ImgUpload.PostedFile != null && ImgUpload.PostedFile.ContentLength > 0)
                        {
                            string fname = Path.GetFileName(ImgUpload.PostedFile.FileName);
                            ImgUpload.SaveAs(Server.MapPath(Path.Combine("~/MasterPage/Uploads/", fname)));
                            ImgUser.ImageUrl = "~/MasterPage/Uploads/" + fname;

                            memberManger.UserImage = "/" + fname;
                        }
                        else
                        {
                            memberManger.UserImage = ImgUser.ImageUrl.Replace("~/MasterPage/Uploads/", "");
                        }


                        string inputJson = (new JavaScriptSerializer()).Serialize(memberManger);
                        client.UploadString(apiUrlUser + "/PutUser?id=" + Convert.ToInt32(ViewState["UserID"]), inputJson);
                        Response.Redirect("MemberMangement.aspx?id=redirectUpdate");
                    }
                    else
                    {
                        bool phoneExist = WebApiConsume.ValidateUserPhone(txtPhoneNumber.Text.Trim().ToLower());
                        if (!phoneExist)
                        {
                            memberManger.Phone = txtPhoneNumber.Text.Trim().ToLower();
                            if (ImgUpload.PostedFile != null && ImgUpload.PostedFile.ContentLength > 0)
                            {
                                string fname = Path.GetFileName(ImgUpload.PostedFile.FileName);
                                ImgUpload.SaveAs(Server.MapPath(Path.Combine("~/MasterPage/Uploads/", fname)));
                                ImgUser.ImageUrl = "~/MasterPage/Uploads/" + fname;

                                memberManger.UserImage = "/" + fname;
                            }
                            else
                            {
                                memberManger.UserImage = ImgUser.ImageUrl;
                            }
                            string inputJson = (new JavaScriptSerializer()).Serialize(memberManger);

                            client.UploadString(apiUrlUser + "/PutUser?id=" + Convert.ToInt32(ViewState["UserID"]), inputJson);
                            Response.Redirect("MemberMangement.aspx?id=redirectUpdate");
                        }
                        else
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "toastr_message", "toastr.success('هذا الرقم موجود من قبل .من فضلك قك بتسجيل رقم اخر')", true);
                        }
                    }
                }
                else
                {
                    Committee.Models.User memberUpdate = new Models.User()
                    {
                        ID          = Convert.ToInt32(Request.QueryString["mId"]),
                        Name        = txtMemberName.Text,
                        UserEmailId = txtMemberEmail.Text,

                        Title               = txtMemberJob.Text,
                        SecrtaryOfDept      = Session["DeptId"].ToString(),
                        WorkSide            = txtWorkSide.Text,
                        SystemRole          = Convert.ToInt32(ddlMemberRole.SelectedItem.Value),
                        UserName            = txtUserName.Text,
                        UserPassword        = Encryptor.MD5Hash(txtPass.Text),
                        Address             = txtAddress.Text,
                        ManagerOfDepartment = Session["DeptId"].ToString(),

                        // UserImage = "/" + ImgUpload?.PostedFile?.FileName
                    };
                    if (string.IsNullOrEmpty(txtUserName.Text))
                    {
                        memberUpdate.UserName = memberUpdate.Name;
                    }
                    if (ViewState["Phone"].ToString() == txtPhoneNumber.Text.Trim().ToLower())
                    {
                        memberUpdate.Phone = txtPhoneNumber.Text.Trim().ToLower();
                        if (ImgUpload.PostedFile != null && ImgUpload.PostedFile.ContentLength > 0)
                        {
                            string fname = Path.GetFileName(ImgUpload.PostedFile.FileName);
                            ImgUpload.SaveAs(Server.MapPath(Path.Combine("~/MasterPage/Uploads/", fname)));
                            ImgUser.ImageUrl = "~/MasterPage/Uploads/" + fname;

                            memberUpdate.UserImage = "/" + fname;
                        }
                        else
                        {
                            memberUpdate.UserImage = ImgUser.ImageUrl.Replace("~/MasterPage/Uploads/", "");
                        }


                        string inputJson = (new JavaScriptSerializer()).Serialize(memberUpdate);
                        client.UploadString(apiUrlUser + "/PutUser?id=" + Convert.ToInt32(ViewState["UserID"]), inputJson);
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "toastr_message", "toastr.success('تم تعديل بيانات العضو بنجاح', 'تم')", true);
                    }
                    else
                    {
                        bool phoneExist = WebApiConsume.ValidateUserPhone(txtPhoneNumber.Text.Trim().ToLower());
                        if (!phoneExist)
                        {
                            memberUpdate.Phone = txtPhoneNumber.Text.Trim().ToLower();
                            if (ImgUpload.PostedFile != null && ImgUpload.PostedFile.ContentLength > 0)
                            {
                                string fname = Path.GetFileName(ImgUpload.PostedFile.FileName);
                                ImgUpload.SaveAs(Server.MapPath(Path.Combine("~/MasterPage/Uploads/", fname)));
                                ImgUser.ImageUrl = "~/MasterPage/Uploads/" + fname;

                                memberUpdate.UserImage = "/" + fname;
                            }
                            else
                            {
                                memberUpdate.UserImage = ImgUser.ImageUrl;
                            }
                            string inputJson = (new JavaScriptSerializer()).Serialize(memberUpdate);

                            client.UploadString(apiUrlUser + "/PutUser?id=" + Convert.ToInt32(ViewState["UserID"]), inputJson);
                            Response.Redirect("MemberMangement.aspx?id=redirectUpdate");
                        }
                        else
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "toastr_message", "toastr.success('هذا الرقم موجود من قبل .من فضلك قك بتسجيل رقم اخر')", true);
                        }
                    }
                }
            }

            else if (Request.QueryString["status"] == "new")
            {
                bool phoneExist = WebApiConsume.ValidateUserPhone(txtPhoneNumber.Text.Trim().ToLower());
                if (!phoneExist)
                {
                    if (ImgUpload.PostedFile != null && ImgUpload.PostedFile.ContentLength > 0)
                    {
                        string fname = Path.GetFileName(ImgUpload.PostedFile.FileName);
                        ImgUpload.SaveAs(Server.MapPath(Path.Combine("~/MasterPage/Uploads/", fname)));
                    }


                    string apiUrlMember = Utilities.BASE_URL + "/api/Users";

                    if (Session["SystemRole"].ToString() == "1")
                    {
                        int    systemRole = 0;
                        string secRole    = null;
                        if (ddlMemberType.SelectedItem.Value == "4")
                        {
                            systemRole = 4;
                            secRole    = ddlmangerForDept?.SelectedItem?.Value;
                        }
                        if (ddlMemberType.SelectedItem.Value == "5")
                        {
                            systemRole = 5;
                            secRole    = null;
                        }
                        Committee.Models.User memberManger = new Models.User()
                        {
                            Name                = txtMemberName.Text,
                            UserEmailId         = txtMemberEmail.Text,
                            Phone               = txtPhoneNumber.Text,
                            Title               = txtMemberJob.Text,
                            SecrtaryOfDept      = secRole,
                            WorkSide            = txtWorkSide.Text,
                            SystemRole          = systemRole,
                            UserName            = txtUserName.Text,
                            UserPassword        = Encryptor.MD5Hash(txtPasswordOfManager.Text),
                            Address             = txtAddress.Text,
                            UserImage           = "/" + ImgUpload?.PostedFile?.FileName,
                            ManagerOfDepartment = ddlmangerForDept?.SelectedItem?.Value,
                        };

                        if (string.IsNullOrEmpty(txtuserNameOfManager.Text))
                        {
                            memberManger.UserName = memberManger.Name;
                        }
                        else
                        {
                            memberManger.UserName = txtuserNameOfManager.Text;
                        }
                        string inputJsonMember = (new JavaScriptSerializer()).Serialize(memberManger);


                        client.UploadString(apiUrlMember + "/PostUser", inputJsonMember);
                    }
                    else
                    {
                        Committee.Models.User member = new Models.User()
                        {
                            Name                = txtMemberName.Text,
                            UserEmailId         = txtMemberEmail.Text,
                            Phone               = txtPhoneNumber.Text,
                            Title               = txtMemberJob.Text,
                            SecrtaryOfDept      = ddlmangerForDept?.SelectedItem?.Value,
                            WorkSide            = txtWorkSide.Text,
                            SystemRole          = 6,
                            UserName            = txtUserName.Text,
                            UserPassword        = Encryptor.MD5Hash(txtPass.Text),
                            Address             = txtAddress.Text,
                            UserImage           = "/" + ImgUpload?.PostedFile?.FileName,
                            ManagerOfDepartment = Session["DeptId"].ToString(),
                        };
                        if (string.IsNullOrEmpty(txtUserName.Text))
                        {
                            member.UserName = member.Name;
                        }
                        string inputJson = (new JavaScriptSerializer()).Serialize(member);


                        client.UploadString(apiUrlMember + "/PostUser", inputJson);
                    }



                    ResetControld();
                    Response.Redirect("MemberMangement.aspx?id=redirectSave");
                }
                else
                {
                    // Page.ClientScript.RegisterStartupScript(this.GetType(), "toastr_message", "toastr.success('هذا الرقم موجود من قبل .من فضلك قك بتسجيل رقم اخر', 'تم')", true);
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "تم", "alert('هذا الرقم موجود من قبل .من فضلك قك بتسجيل رقم اخر');", true);
                }
            }
        }