在数据库中添加链接的地址:
通过iframe中的name属性指定打开页面:
cn.Target = "MainFrame";
页面的布局:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WorkerAdd.aspx.cs" Inherits="BioErpWeb.HRSystem.WorkerAdd" %> <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link href="../Styles/ERPBaseStyle.css" rel="stylesheet" type="text/css" /> <link href="../Styles/CalenderStyle.css" rel="stylesheet" type="text/css" /> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <div> <table class="maintable"> <tr> <td colspan="4" class="titlebar"><span>员工入职登记</span></td> </tr> <tr> <td>登录名</td><td><asp:TextBox ID="txtLoginName" runat="server"></asp:TextBox></td> <td>真实姓名</td><td><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td> </tr> <tr> <td>所属部门</td><td> <asp:DropDownList ID="ddlDepartMent" runat="server"> </asp:DropDownList> </td> <td>角色</td> <td><asp:DropDownList ID="ddlRose" runat="server"> </asp:DropDownList></td> </tr> <tr> <td> 生日 </td> <td> <asp:TextBox ID="txtBirthday" runat="server"></asp:TextBox> <cc1:CalendarExtender runat="server" TargetControlID="txtBirthday" Format="yyyy-MM-dd"> </cc1:CalendarExtender> </td> <td> 手机号码 </td> <td> <asp:TextBox ID="txtMobile" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Email地址 </td> <td> <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox> </td> <td> 照片 </td> <td> <asp:FileUpload ID="fp_photo" runat="server" /> <asp:Button ID="btnPhoto" Width="50" runat="server" Text="上传" onclick="btnPhoto_Click" /> <asp:Image ID="Userimg" runat="server" Width="100" Height="110" /> </td> </tr> <tr> <td> 住址 </td> <td> <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox> </td> <td> 性别 </td> <td> <asp:DropDownList ID="ddlSex" runat="server"> <asp:ListItem Value="0">男</asp:ListItem> <asp:ListItem Value="1">女</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td> 显示的顺序 </td> <td> <asp:TextBox ID="txtDisplayOrder" runat="server"></asp:TextBox> </td> <td> 员工状态 </td> <td> <asp:DropDownList ID="ddlState" runat="server"> <asp:ListItem Value="1">在职</asp:ListItem> <asp:ListItem Value="0">离职</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td colspan="4" class="bottomtd"> <asp:Button ID="btnSubmit" runat="server" Text="员工信息登记" CssClass="submitbutton" onclick="btnSubmit_Click"/> </td> </tr> </table> <br /> </div> </form> </body> </html>
在NuGet中执行:
Install-Package AjaxControlToolkit
CSS样式表代码:
body { PADDING-RIGHT: 0px; PADDING-LEFT: 0px; BACKGROUND: #dde4ea; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px; FONT-FAMILY: Tahoma,Arial,Helvetica,Sans-Serif; font-size:12px; } .maintable{ width:800px; border:solid 1px #8fabc1; border-collapse:collapse; margin:0px auto;} .maintable td{ border:solid 1px #8fabc1;} .titlebar{ font-size:14px; font-weight:bold; line-height:30px; background-color:#8fabc1; text-align:center;} input{ width:150px; border:solid 1px #ccc;} input:hover{ border:solid 1px red;} .submitbutton{ width:100px;} .bottomtd{ text-align:right; padding-right:20px;}
日历控件的CSS:
.MyCalendar .ajax__calendar_container { border:1px solid #646464; background-color:#faac38; } .MyCalendar .ajax__calendar_other .ajax__calendar_day, .MyCalendar .ajax__calendar_other .ajax__calendar_year { color:#ffffff; } .MyCalendar .ajax__calendar_hover .ajax__calendar_day { color:red; background-color:#e8e8e8; } .MyCalendar .ajax__calendar_active .ajax__calendar_day { color:blue; font-weight:bolder; background-color:#e8e8e8; }
样式:
绑定部门的存储过程:
-- ============================================= -- Author: 孙丽媛 -- Create date: 2016/10/07 -- Description: 查询部门信息 -- ============================================= ALTER PROCEDURE [dbo].[getDepartMent] AS BEGIN SELECT * FROM Department ORDER BY Displayorder asc END
绑定角色存储过程:
-- ============================================= -- Author: 孙丽媛 -- Create date: 2016年10月07日 -- Description: 获取员工所有角色信息 -- ============================================= ALTER PROCEDURE [dbo].[getUserRoseList] AS BEGIN SELECT RoseID, RoseName FROM tbRose END
公用层的封装:
/// <summary> /// 查询部门信息 /// </summary> /// <returns></returns> public static DataTable getDepartMent() { return DataBaseHelper.SelectSQLReturnTable("getDepartMent", CommandType.StoredProcedure); } /// <summary> /// 获取员工角色列表 /// </summary> /// <returns>DataTable</returns> public static DataTable getUserRoseList() { return DataBaseHelper.SelectSQLReturnTable("getUserRoseList", CommandType.StoredProcedure); }
后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DepartMentBand(); UserRoseList(); } } /// <summary> /// 绑定部门信息 /// </summary> private void DepartMentBand() { ddlDepartMent.DataSource= SqlComm.getDepartMent(); //显示的列 ddlDepartMent.DataTextField = "DepartmentName"; //隐藏的列 ddlDepartMent.DataValueField = "DepartmentId"; ddlDepartMent.DataBind(); ddlDepartMent.Items.Add(new ListItem("--请选择部门--", "0")); ddlDepartMent.SelectedValue = "0"; } /// <summary> /// 员工角色列表 /// </summary> private void UserRoseList() { ddlRose.DataSource= SqlComm.getUserRoseList(); ddlRose.DataTextField = "RoseName"; ddlRose.DataValueField = "RoseID"; ddlRose.DataBind(); ddlRose.Items.Add(new ListItem("--请选择角色--", "0")); ddlRose.SelectedValue = "0"; }
员工添加的存储过程:
ALTER PROCEDURE [dbo].[UserManager_ADD] @LoginName nvarchar(50), @UserName nvarchar(50), @Password nvarchar(50), @DepartmentId int, @RoleId int, @Birthday datetime, @Mobile nvarchar(50), @Email nvarchar(50), @Photo nvarchar(20), @Address nvarchar(50), @LastLoginDate datetime, @Sex bit, @DisplayOrder int, @Sate bit AS INSERT INTO [UserManager]( [LoginName],[UserName],[Password],[DepartmentId],[RoleId],[Birthday],[Mobile],[Email],[Photo],[Address],[LastLoginDate],[Sex],[DisplayOrder],[Sate] )VALUES( @LoginName,@UserName,@Password,@DepartmentId,@RoleId,@Birthday,@Mobile,@Email,@Photo,@Address,@LastLoginDate,@Sex,@DisplayOrder,@Sate )
修改的存储过程:
ALTER PROCEDURE [dbo].[UserManager_Update] @UserId int, @LoginName nvarchar(50), @UserName nvarchar(50), @Password nvarchar(50), @DepartmentId int, @RoleId int, @Birthday datetime, @Mobile nvarchar(50), @Email nvarchar(50), @Photo nvarchar(20), @Address nvarchar(50), @LastLoginDate datetime, @Sex bit, @DisplayOrder int, @Sate bit AS UPDATE [UserManager] SET [LoginName] = @LoginName,[UserName] = @UserName,[Password] = @Password,[DepartmentId] = @DepartmentId,[RoleId] = @RoleId,[Birthday] = @Birthday,[Mobile] = @Mobile,[Email] = @Email,[Photo] = @Photo,[Address] = @Address,[LastLoginDate] = @LastLoginDate,[Sex] = @Sex,[DisplayOrder] = @DisplayOrder,[Sate] = @Sate WHERE [email protected]
业务逻辑层:
public class UserManagerBLL { /// <summary> /// 添加员工信息 /// </summary> /// <param name="user">user</param> public int UserMangerAdd(UserManager user) { SqlParameter[] pars = new SqlParameter[]{ new SqlParameter("@LoginName",user.LoginName), new SqlParameter("@UserName",user.UserName), new SqlParameter("@Password",user.Password), new SqlParameter("@DepartmentId",user.DepartmentId), new SqlParameter("@RoleId",user.RoleId), new SqlParameter("@Birthday",user.Birthday), new SqlParameter("@Mobile",user.Mobile), new SqlParameter("@Email",user.Email), new SqlParameter("@Photo",user.Photo), new SqlParameter("@Address",user.Address), new SqlParameter("@LastLoginDate",user.LastLoginDate), new SqlParameter("@Sex",user.Sex), new SqlParameter("@DisplayOrder",user.DisplayOrder), new SqlParameter("@Sate",user.Sate) }; return DataBaseHelper.ExcuteSqlReturnInt("UserManager_ADD", CommandType.StoredProcedure, pars); } /// <summary> /// 员工修改 /// </summary> /// <param name="user">员工对象</param> /// <returns>int</returns> public int UserManagerUpdate(UserManager user) { SqlParameter[] pars = new SqlParameter[]{ new SqlParameter("@UserId",user.UserId), new SqlParameter("@LoginName",user.LoginName), new SqlParameter("@UserName",user.UserName), new SqlParameter("@Password",user.Password), new SqlParameter("@DepartmentId",user.DepartmentId), new SqlParameter("@RoleId",user.RoleId), new SqlParameter("@Birthday",user.Birthday), new SqlParameter("@Mobile",user.Mobile), new SqlParameter("@Email",user.Email), new SqlParameter("@Photo",user.Photo), new SqlParameter("@Address",user.Address), new SqlParameter("@LastLoginDate",user.LastLoginDate), new SqlParameter("@Sex",user.Sex), new SqlParameter("@DisplayOrder",user.DisplayOrder), new SqlParameter("@Sate",user.Sate) }; return DataBaseHelper.ExcuteSqlReturnInt("UserManager_Update", CommandType.StoredProcedure, pars); }
后台代码:
protected void btnSubmit_Click(object sender, EventArgs e) { UserManager user = new UserManager(); user.LoginName = this.txtLoginName.Text; user.UserName = this.txtUserName.Text; if (ddlDepartMent.SelectedValue == "0") { //Response.Write("<script>alert(‘请选择部门‘)</script>"); ScriptManager.RegisterStartupScript(this, this.GetType(), "test", "alert(‘请选择部门‘);", true); return; } else { user.DepartmentId = int.Parse(ddlDepartMent.SelectedValue.ToString()); } if (ddlRose.SelectedValue == "0") { ScriptManager.RegisterStartupScript(this, this.GetType(), "test", "alert(‘请选择角色‘);", true); return; } else { user.RoleId =int.Parse(ddlRose.SelectedValue.ToString()); } user.Mobile = this.txtMobile.Text; user.Birthday = Convert.ToDateTime(this.txtBirthday.Text); user.Email = this.txtEmail.Text; user.Address = this.txtAddress.Text; //0:男,1,女 user.Sex = this.ddlSex.SelectedValue == "0" ? true : false; user.DisplayOrder = Convert.ToInt32(this.txtDisplayOrder.Text); //0:离职,1:在职 user.Sate = this.ddlState.SelectedValue == "0" ? false : true; user.Password =Comm.MD5("123456"); user.Photo = ImgName; user.LastLoginDate =Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")); UserManagerBLL userbll = new UserManagerBLL(); int count=userbll.UserMangerAdd(user); if (count ==0) { ScriptManager.RegisterStartupScript(this, this.GetType(), "test", "alert(‘数据提交失败‘);", true); return; } }
图像上传:(先新建一个文件夹)
思路:
//图片上传(第一种方法) protected void btnPhoto_Click(object sender, EventArgs e) { //获取名字 string filename= this.fp_photo.FileName; //取出文件得后缀名 string filetype = filename.Substring(filename.LastIndexOf(".")+1); //给出提示 if (filetype.ToLower() != "jpg" && filetype.ToLower() != "bmp" && filetype.ToLower() != "gif" && filetype.ToLower() != "png") { ScriptManager.RegisterStartupScript(this, this.GetType(), "test", "alert(‘请选择有效的图片格式(*.jpg/*.bmp/*.gif/*.png)‘);", true); return; } //找到保存图片的物理路径 string filepath= Server.MapPath(@"\Files\Usersphoto\") + filename; //另存为上述的路径 this.fp_photo.SaveAs(filepath); //从文件夹中取出图片进行显示 this.Userimg.ImageUrl = @"\Files\Usersphoto\" + filename; }
效果:
上传文件调用公用的方法:
static string ImgName = ""; //图片上传 protected void btnPhoto_Click(object sender, EventArgs e) { string filename= this.fp_photo.FileName; if (!Comm.isImg(filename)) { ScriptManager.RegisterStartupScript(this, this.GetType(), "test", "alert(‘请选择有效的图片格式(*.jpg/*.bmp/*.gif/*.png)‘);", true); return; } string filepath = Server.MapPath(@"\Files\Usersphoto\"); //上传指定路径的文件 ImgName= Comm.FileUpLoad(this.fp_photo, filepath); if (ImgName == "false") { ScriptManager.RegisterStartupScript(this, this.GetType(), "test", "alert(‘上传失败‘)", true); return; } this.Userimg.ImageUrl = @"\Files\Usersphoto\" + ImgName; }
封装成通用得方法:
引用命名空间:using System.Web.UI.WebControls;
public class Comm { /// <summary> /// 上传文件,文件名要求唯一,上传成功显示文件名,如123.jpg,否则返回 false; /// </summary> /// <param name="fload">上传组件</param> /// <param name="path">指定的路径 ,总是以“/”结尾</param> /// <returns>string</returns> public static string FileUpLoad(FileUpload fload, string path) { //判断指定路径是否存在此目录,如果没有则创建 if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } int count=0; //获取文件名称 string fileName = fload.FileName; //获取新的文件名称 string NewFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + fileName; //上传文件之前判断指定路径是否已经存在此文件,如果存在,则删除 if (System.IO.File.Exists(path + NewFileName)) { System.IO.File.Delete(path + NewFileName); } try { fload.SaveAs(path + NewFileName); } catch (Exception ex) { count++; } if (count > 0) { return "false"; } else { return NewFileName; } } /// <summary> /// 通用的删除文件功能 /// </summary> /// <param name="path"></param> public static bool FileDelete(string path) { if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); return true; } return false; } /// <summary> /// 判断指定路径是否存在文件 /// </summary> /// <param name="path">路径</param> /// <returns>bool</returns> public static bool FileExists(string path) { if (System.IO.File.Exists(path)) { return true; } return false; } /// <summary> /// 对指定字符串加密 /// </summary> /// <param name="pwd">字符串</param> /// <returns>string</returns> public static string MD5(string pwd) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "md5"); } /// <summary> /// 是否是有效的图片格式 ,如jpg,bmp,gif,png /// </summary> /// <param name="FileName">文件名带后缀名</param> /// <returns>string</returns> public static bool isImg(string FileName) { string filetype = FileName.Substring(FileName.LastIndexOf(".") + 1); if (filetype.ToLower() != "jpg" && filetype.ToLower() != "bmp" && filetype.ToLower() != "gif" && filetype.ToLower() != "png") { return false; } return true; } public static bool isIge(string FileName) { string filetype = FileName.Substring(FileName.LastIndexOf(".") + 1); if (filetype.ToLower() != "jpg" && filetype.ToLower() != "bmp" && filetype.ToLower() != "gif" && filetype.ToLower() != "png") { return false; } return true; } }
时间: 2024-10-21 11:26:44