简单的人员管理系统
展示页面
添加人员
--判断添加人员的各种条件限制
-- 各种提示
修改人员信息
-- 人员原来信息绑定
--密码不显示,密码不改时用原来密码
人员删除
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// users 的摘要说明 /// </summary> public class users { public int Ids { get; set; } public string Username { get; set; } public string Password { get; set; } public string Nickname { get; set; } public bool Sex { get; set; } public string SexStr { get { return Sex ? "男" : "女"; } } public DateTime Birthday { get; set; } public string Birthdaystr { get { return Birthday.ToString("yyyy年MM月dd日"); } } public int Age { get { return DateTime.Now.Year - Birthday.Year; } } public string Nation { get; set; } public string NationName { get { return new usernationData().selectname(Nation); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; /// <summary> /// usersData 的摘要说明 /// </summary> public class usersData { SqlConnection conn = null; SqlCommand cmd = null; public usersData() { conn = new SqlConnection("server=.;database=Data0216_5;user=sa;pwd=123"); cmd = conn.CreateCommand(); } //查询全部的人员信息 public List<users> selectAll() { List<users> ulist = new List<users>(); cmd.CommandText = "select * from users"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { users u = new users(); u.Ids = Convert.ToInt32(dr[0]); u.Username = dr[1].ToString(); u.Password = dr[2].ToString(); u.Nickname = dr[3].ToString(); u.Sex = Convert.ToBoolean(dr[4]); u.Birthday = Convert.ToDateTime(dr[5]); u.Nation = dr[6].ToString(); ulist.Add(u); } } conn.Close(); return ulist; } //添加记录的方法 public bool insertuser( users u) { bool b = false ; cmd.CommandText = "insert into users values(@a,@b,@c,@d,@e,@f)"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@a",u.Username); cmd.Parameters.AddWithValue("@b", u.Password ); cmd.Parameters.AddWithValue("@c", u.Nickname ); cmd.Parameters.AddWithValue("@d", u.Sex ); cmd.Parameters.AddWithValue("@e", u.Birthday ); cmd.Parameters.AddWithValue("@f", u.Nation ); conn.Open(); int a = cmd.ExecuteNonQuery(); if (a >= 1) b = true; conn.Close(); return b; } //删除 public int deleteuser(string id) { int end = 0; cmd.CommandText = "delete from users where Ids = @a"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@a", id); conn.Open(); end = cmd.ExecuteNonQuery(); conn.Close(); return end; } //修改 public int updateuser(users u) { int end = 0; cmd.Parameters.Clear(); if (u.Password.Length > 0) { cmd.CommandText = "update users set PassWord=@a,NickName=@b,Sex=@c,Birthday=@d,Nation=@e where Ids = @f"; cmd.Parameters.AddWithValue("@a", u.Password); } else { cmd.CommandText = "update users set NickName=@b,Sex=@c,Birthday=@d,Nation=@e where Ids = @f"; } cmd.Parameters.AddWithValue("@b", u.Nickname); cmd.Parameters.AddWithValue("@c", u.Sex); cmd.Parameters.AddWithValue("@d", u.Birthday); cmd.Parameters.AddWithValue("@e", u.Nation); cmd.Parameters.AddWithValue("@f", u.Ids); conn.Open(); end = cmd.ExecuteNonQuery(); conn.Close(); return end; } //根据ids查用户 public users selectuser(string ids) { users u = null; cmd.CommandText = "select *from users where Ids = @a"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a", ids); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { u = new users(); dr.Read(); u.Ids = Convert.ToInt32(dr[0]); u.Username = dr[1].ToString(); u.Password = dr[2].ToString(); u.Nickname = dr[3].ToString(); u.Sex = Convert.ToBoolean(dr[4]); u.Birthday = Convert.ToDateTime(dr[5]); u.Nation = dr[6].ToString(); } conn.Close(); return u; } //根据即时用户名查是否存在 public users selectname(string username) { users u = null; cmd.CommandText = "select *from users where Username = @a"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a", username); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { u = new users(); dr.Read(); u.Ids = Convert.ToInt32(dr[0]); u.Username = dr[1].ToString(); u.Password = dr[2].ToString(); u.Nickname = dr[3].ToString(); u.Sex = Convert.ToBoolean(dr[4]); u.Birthday = Convert.ToDateTime(dr[5]); u.Nation = dr[6].ToString(); } conn.Close(); return u; } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// usernation 的摘要说明 /// </summary> public class usernation { public string NationCode { get; set; } public string NationName { get; set; } }
using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Web; /// <summary> /// usernationData 的摘要说明 /// </summary> public class usernationData { SqlConnection conn = null; SqlCommand cmd = null; public usernationData() { conn = new SqlConnection("server=.;database=Data0216_5;user=sa;pwd=123"); cmd = conn.CreateCommand(); } //查询所有的民族信息 public List<usernation> selectAll() { List<usernation> ulist = new List<usernation>(); cmd.CommandText = "select *from usernation"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { usernation u = new usernation(); u.NationCode = dr["NationCode"].ToString(); u.NationName = dr["NationName"].ToString(); ulist.Add(u); } conn.Close(); return ulist; } //根据民族编号查询民族名称 public string selectname(string code) { string s = null; cmd.CommandText = "select NationName from usernation where NationCode=@a "; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@a", code); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { dr.Read(); s = dr[0].ToString(); } conn.Close(); return s; } }
---------------------------------------------------------------------------------------------
1、数据展示页
效果图
主页
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> .div1 { width: 100%; height: 80px; text-align: center; line-height: 80px; font-size: 30px; } /*表格样式*/ .tab { width: 100%; background-color: blue; text-align: center; } </style> </head> <body> <form id="form1" runat="server"> <div class="div1">奇点0216班学生信息</div> <%--使用 Repeater 添加数据--%> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <%-- 头模板--%> <table class="tab"> <tr style="color: white; height: 30px;"> <td>编号</td> <td>用户名</td> <td>密码</td> <td>昵称</td> <td>性别</td> <td>生日</td> <td>年龄</td> <td>民族</td> <td>设置</td> </tr> </HeaderTemplate> <ItemTemplate> <%-- 项模板--%> <tr style="background-color: white;"> <td><%#Eval("Ids") %></td> <td><%#Eval("Username") %></td> <td><%#Eval("Password") %></td> <td><%#Eval("Nickname") %></td> <td><%#Eval("Sexstr") %></td> <td><%#Eval("Birthdaystr") %></td> <td><%#Eval("Age") %></td> <td><%#Eval("NationName") %></td> <td> <a href="xiugai.aspx?i=<%#Eval("Ids") %>">编辑 </a> <a οnclick="return confirm('是否要删除<%#Eval("NickName") %>?');" href="shanchu.aspx?i=<%#Eval("Ids") %>">删除</a> </td> </tr> </ItemTemplate> <FooterTemplate> <%--脚模板--%> </table> </FooterTemplate> </asp:Repeater> <a href="zhuce.aspx" target="_blank">添加新同学</a> </form> </body> </html>
后台
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //只在第一次加载时执行的代码 //数据展示时不需要,数据展示要展示最新数据 } Repeater1.DataSource = new usersData().selectAll(); Repeater1.DataBind(); } }
------------------------------------------------------------------------------------------
2、添加页
效果图
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="zhuce.aspx.cs" Inherits="zhuce" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <h1 style="text-align: center">用户注册</h1> <div style="position: absolute; left: 550px; top: 100px"> <%--使用表格布局--%> <table style="text-align: left"> <tr> <td style="text-align: right">用户名: </td> <td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> <td><%--使用label提示用户名错误信息--%> <asp:Label ID="uname_error" runat="server" Text="Label"></asp:Label> </td> </tr> <tr> <td style="text-align: right">昵称:</td> <td> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td> <td><%--使用label提示昵称错误信息--%> <asp:Label ID="nick_error" runat="server" Text="Label"></asp:Label> </td> </tr> <tr> <td style="text-align: right">密码:</td> <td> <asp:TextBox ID="p1" TextMode="Password" runat="server"></asp:TextBox> </td> <td></td> </tr> <tr> <td style="text-align: right">确认密码:</td> <td> <asp:TextBox ID="p2" TextMode="Password" runat="server"></asp:TextBox> </td> <td><%--使用label提示密码错误信息--%> <asp:Label ID="pwd_error" runat="server" Text="Label"></asp:Label> </td> </tr> <tr> <td style="text-align: right">性别:</td> <td> <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal"> <asp:ListItem Text="男" Value="true" Selected="True"></asp:ListItem> <asp:ListItem Text="女" Value="false"></asp:ListItem> </asp:RadioButtonList> </td> <td></td> </tr> <tr> <td style="text-align: right">生日: </td> <td> <asp:DropDownList ID="Dr1" runat="server"></asp:DropDownList>年 <asp:DropDownList ID="Dr2" runat="server"></asp:DropDownList>月 <asp:DropDownList ID="Dr3" runat="server"></asp:DropDownList>日 </td> <td></td> </tr> <tr> <td style="text-align: right">民族: </td> <td> <asp:DropDownList ID="Dr4" runat="server"></asp:DropDownList> </td> <td></td> </tr> <tr> <td></td> <td> <asp:Button OnClientClick="return go();" ID="Button1" runat="server" Text="提交" /> </td> <td></td> <%--OnClientClick="return go();" 当go()返回false时 按钮无法点击--%> </tr> </table> <asp:Literal ID="Literal1" runat="server"></asp:Literal> </div> </form> </body> </html> <script type="text/javascript"> var pwdok = false; var nickok = false; var nameok = false; //判断两次密码是否一致 var pwd1 = document.getElementById("p1"); var pwd2 = document.getElementById("p2"); pwd1.onkeyup = function () { pwd1_2(pwd1, pwd2); } //pwd1 按键抬起时触发方法 pwd2.onkeyup = function () { pwd1_2(pwd1, pwd2); } //pwd2 按键抬起时触发方法 function pwd1_2(a, b) { // pwd1_2 判断方法名称 if (a.value != b.value) { //表单元素用 value取值,非表单元素用innerText document.getElementById("pwd_error").innerText = "两次密码不一致!"; document.getElementById("pwd_error").style.color = "red"; pwdok = false; } else { document.getElementById("pwd_error").innerText = "正确!"; document.getElementById("pwd_error").style.color = "green"; pwdok = true; } } //判断昵称是否为空 document.getElementById("TextBox2").onkeyup = function () { if (this.value.length > 0) { nickok = true; } else { nickok = false; } } //判断用户名是否为空 document.getElementById("TextBox1").onkeyup = function () { if (this.value.length > 0) { nameok = true; } else { nameok = false; } } //最终返回 false 还是 true ———— 确定按钮是否可用 function go() { return pwdok && nickok && nameok; } //年份改变时发生 //onchange 内容改变触发 var year = document.getElementById("Dr1"); var mon = document.getElementById("Dr2"); var day = document.getElementById("Dr3"); year.onchange = function () { if (mon.value == "2") { if (this.value % 4 == 0 && this.value % 100 != 0 || this.value % 400 == 0) { // 选中的年份 day.options.length = 0; //下拉列表中值变为空 for (var i = 1; i < 30; i++) { var op = document.createElement("option");// 创建option 元素 op.value = i; op.innerHTML = i; day.appendChild(op); //将op 代表的值放到 option 中去 } } else { day.options.length = 0; for (var i = 1; i < 29; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } } } //月份改变时发生 mon.onchange = function () { if (this.value == "2") { if (year.value % 4 == 0 && year.value % 100 != 0 || year.value % 400 == 0) { day.options.length = 0; //下拉列表中值变为空 for (var i = 1; i < 30; i++) { var op = document.createElement("option");// 创建option 元素 op.value = i; op.innerHTML = i; day.appendChild(op); //将op 代表的值放到 option 中去 } } else { day.options.length = 0; for (var i = 1; i < 29; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } } else if (this.value == "1" || this.value == "3" || this.value == "5" || this.value == "7" || this.value == "8" || this.value == "10" || this.value == "12") { day.options.length = 0; for (var i = 1; i < 32; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } else if (this.value == "4" || this.value == "6" || this.value == "9" || this.value == "11") { day.options.length = 0; for (var i = 1; i < 31; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } } </script>
后台
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class zhuce : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; if (!IsPostBack) //在第一次加载时添加 { //添加年月日 for (int i = DateTime.Now.Year; i >= 1960; i--) { ListItem li = new ListItem(i.ToString(), i.ToString()); Dr1.Items.Add(li); } for (int i = 1; i < 13; i++) { Dr2.Items.Add(new ListItem(i.ToString(), i.ToString())); } for (int i = 1; i <= 31; i++) { Dr3.Items.Add(new ListItem(i.ToString(), i.ToString())); } //添加民族 Dr4.DataSource = new usernationData().selectAll(); Dr4.DataTextField = "NationName"; Dr4.DataValueField = "NationCode"; Dr4.DataBind(); } } //确定按钮 —— 确定添加 void Button1_Click(object sender, EventArgs e) { users us = new usersData().selectname(TextBox1.Text); if (us != null) { uname_error.Text = "用户名已存在"; return; } users u = new users(); u.Username = TextBox1.Text.Trim(); u.Nickname = TextBox2.Text; u.Password = p1.Text; u.Sex = RadioButtonList1.Items[0].Selected; //性别 u.Birthday = Convert.ToDateTime(Dr1.SelectedValue + "-" + Dr2.SelectedValue + "-" + Dr3.SelectedValue); u.Nation = Dr4.SelectedValue; if (new usersData().insertuser(u)) { Response.Redirect("zhuceok.aspx?a=1"); } else { Response.Redirect("zhuceok.aspx?a=0"); } } }
注意
本例时间缺少一个条件判断,现在只能按 年-月-日 的顺序选)
添加密码时,没有对无密码行为进行限制
重点:时间选择由3个下拉列表组成,年月日的判断最好写 JS
也可以 用AutoPostBack 与 SelectedIndexChanged 配合,但是每次选择都会刷新,密码会清空。
OnClick -- C#的点击事件
OnClientClick -- JS 的点击事件
OnClientClick="return false" --无法进行点击(css)
AutoPostBack=" true "; -- 自动提交( css )
SelectedIndexChanged -- 选项改变事件 ( C#)
AutoPostBack 与 SelectedIndexChanged 相配合使用 在后台进行使用
Option -- 下拉列表中的一个选项。
document.createElement("BUTTON"); -- 创建一个按钮
例:创建一个指定文本的按钮
var btn=document.createElement("BUTTON");
var t=document.createTextNode("CLICK ME");
btn.appendChild(t);
-----------------------------------------------------------------------------------------------------------------
3、提示页
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="zhuceok.aspx.cs" Inherits="zhuceok" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> .div1 { position: absolute; margin-top: 80px; left: 400px; width: 650px; height: 400px; text-align: center; line-height: 80px; font-size: 30px; } </style> </head> <body> <form id="form1" runat="server"> <div class="div1"> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br /> <span id="sp1">10</span>秒后自动返回主页面,如未返回请<a href="Default.aspx">点击此处</a>。 </div> </form> </body> </html> <script type="text/javascript"> var sss = 10; document.getElementById("sp1").innerHTML = sss; window.setInterval(function () { //开定时器 if (sss < 1) { window.location.href = "Default.aspx"; } sss--; document.getElementById("sp1").innerHTML = sss; }, 1000); </script>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class zhuceok : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string s = Request["a"]; if (s == "1") { Label1.Text = "恭喜你!注册成功!!!"; Label1.ForeColor = System.Drawing.Color.Red; } else { Label1.Text = "注册失败!!!!!"; Label1.ForeColor = System.Drawing.Color.Red; } } }
--------------------------------------------------------------------------------------
4、删除
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="shanchu.aspx.cs" Inherits="shanchu" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Literal ID="Literal1" runat="server"></asp:Literal> </div> </form> </body> </html>
public partial class shanchu : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string s = Request["i"]; int a = new usersData().deleteuser(s); //if (a > 0) //{ // Literal1.Text = "<script>alert('删除成功!');window.location.href='Default.aspx';</script>"; //} //else //{ // Literal1.Text = "<script>alert('删除失败!');window.location.href='Default.aspx';</script>"; //} Response.Redirect("Default.aspx"); } }
<a οnclick="return confirm('是否要删除<%#Eval("NickName") %>?');" href="shanchu.aspx?i=<%#Eval("Ids") %>">删除</a> //展示页,《删除》的设置
οnclick="return confirm( )"; -- 点击时弹出 ‘ 确定对话框 ’ ,点击确定后再执行后面的命令
Literal1.Text = "<script>alert('删除成功!');window.location.href='Default.aspx';</script>";
-- 先弹出对话框,确定后再跳转页面
----------------------------------------------------------------------------------------------
5、修改
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xiugai.aspx.cs" Inherits="xiugai" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <h1 style="text-align: center">用户修改</h1> <div style="position: absolute; left: 550px; top: 100px"> <table style="text-align: right"> <tr> <td>用户名: </td> <td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> <td> <asp:Label ID="uname_error" runat="server" Text="Label"></asp:Label> </td> </tr> <tr> <td>昵称:</td> <td> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td> <td> <asp:Label ID="nick_error" runat="server" Text="Label"></asp:Label> </td> </tr> <tr> <td>密码:</td> <td> <asp:TextBox ID="p1" TextMode="Password" runat="server"></asp:TextBox> </td> <td></td> </tr> <tr> <td>确认密码:</td> <td> <asp:TextBox ID="p2" TextMode="Password" runat="server"></asp:TextBox> </td> <td> <asp:Label ID="pwd_error" runat="server" Text="Label"></asp:Label> </td> </tr> <tr> <td>性别:</td> <td> <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal"> <asp:ListItem Text="男" Value="true" Selected="True"></asp:ListItem> <asp:ListItem Text="女" Value="false"></asp:ListItem> </asp:RadioButtonList> </td> <td></td> </tr> <tr> <td>生日: </td> <td> <asp:DropDownList ID="Dr1" runat="server"></asp:DropDownList>年 <asp:DropDownList ID="Dr2" runat="server"></asp:DropDownList>月 <asp:DropDownList ID="Dr3" runat="server"></asp:DropDownList>日 </td> <td></td> </tr> <tr> <td>民族: </td> <td> <asp:DropDownList ID="Dr4" runat="server"></asp:DropDownList> </td> <td></td> </tr> <tr> <td> </td> <td> <asp:Button OnClientClick="return go();" ID="Button1" runat="server" Text="提交" /> </td> <td></td> </tr> </table> </div> </form> </body> </html> <script type="text/javascript"> var pwdok = false; var nickok = false; var nameok = false; //判断两次密码是否一致 var pwd1 = document.getElementById("p1"); var pwd2 = document.getElementById("p2"); pwd1.onkeyup = function () { pwd1_2(pwd1, pwd2); } pwd2.onkeyup = function () { pwd1_2(pwd1, pwd2); } function pwd1_2(a, b) { if (a.value != b.value) { document.getElementById("pwd_error").innerText = "两次密码不一致!"; document.getElementById("pwd_error").style.color = "red"; pwdok = false; } else { document.getElementById("pwd_error").innerText = "正确!"; document.getElementById("pwd_error").style.color = "green"; pwdok = true; } } //判断昵称是否为空 document.getElementById("TextBox2").onkeyup = function () { if (this.value.length > 0) { nickok = true; } else { nickok = false; } } //最终返回 false 还是 true ———— 确定按钮是否可用 function go() { if (document.getElementById("TextBox2").value.length > 0) { nickok = true; } if (document.getElementById("p1").value.length == 0 && document.getElementById("p2").value.length == 0) { pwdok = true; } return pwdok && nickok ; } //去掉用户名为空这个判断 //增加 两个密码框为空时 可以点击 // 再次判断一次昵称不为空时可以点击,之前为 onkeyup 按键抬起触发 //年份改变时发生 var year = document.getElementById("Dr1"); var mon = document.getElementById("Dr2"); var day = document.getElementById("Dr3"); year.onchange = function () { if (mon.value == "2") { if (this.value % 4 == 0 && this.value % 100 != 0 || this.value % 400 == 0) { day.options.length = 0; for (var i = 1; i < 30; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } else { day.options.length = 0; for (var i = 1; i < 29; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } } } //月份改变时发生 mon.onchange = function () { if (this.value == "2") { if (year.value % 4 == 0 && year.value % 100 != 0 || year.value % 400 == 0) { day.options.length = 0; for (var i = 1; i < 30; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } else { day.options.length = 0; for (var i = 1; i < 29; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } } else if (this.value == "1" || this.value == "3" || this.value == "5" || this.value == "7" || this.value == "8" || this.value == "10" || this.value == "12") { day.options.length = 0; for (var i = 1; i < 32; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } else if (this.value == "4" || this.value == "6" || this.value == "9" || this.value == "11") { day.options.length = 0; for (var i = 1; i < 31; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } } </script>
ing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class xiugai : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; if (!IsPostBack) { string s = Request["i"]; users uu = new usersData().selectuser(s); TextBox1.Text = uu.Username; //绑定用户名 TextBox1.ReadOnly = true; //只能看 TextBox2.Text = uu.Nickname; //绑定昵称 if (uu.Sex) //绑定性别 { RadioButtonList1.Items[0].Selected = true; } else { RadioButtonList1.Items[1].Selected = true; } //添加年月日——绑定日期 for (int i = DateTime.Now.Year; i >= 1960; i--) { ListItem li = new ListItem(i.ToString(), i.ToString()); if (li.Text == uu.Birthday.Year.ToString()) { li.Selected = true; } Dr1.Items.Add(li); } for (int i = 1; i < 13; i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); if (li.Text == uu.Birthday.Year.ToString()) { li.Selected = true; } Dr2.Items.Add(li); } for (int i = 1; i <= 31; i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); if (li.Text == uu.Birthday.Year.ToString()) { li.Selected = true; } Dr3.Items.Add(li); } //添加民族 Dr4.DataSource = new usernationData().selectAll(); Dr4.DataTextField = "NationName"; Dr4.DataValueField = "NationCode"; Dr4.DataBind(); foreach (ListItem li in Dr4.Items) //绑定民族 { if (li.Value == uu.Nation) { li.Selected = true; } } } } void Button1_Click(object sender, EventArgs e) { users u = new users(); u.Username = TextBox1.Text.Trim(); u.Nickname = TextBox2.Text; u.Password = p1.Text; u.Sex = RadioButtonList1.Items[0].Selected; //性别 u.Birthday = Convert.ToDateTime(Dr1.SelectedValue + "-" + Dr2.SelectedValue + "-" + Dr3.SelectedValue); u.Nation = Dr4.SelectedValue; u.Ids = Convert.ToInt32(Request["i"]); int a = new usersData().updateuser(u); if (a > 0) { Response.Write("<script>alert('修改成功!!!');window.opener.location.href='Default.aspx';window.close();</script>"); } //Response.Write 在页面最顶端 提示—— —— 返回父级——-——关上本页 else { Response.Write("<script>alert('修改失败!');</script>"); } } }
注意
密码不能显示给用户看,密码不改时沿用原密码(最后在修改方法中判断密码是否修改)
添加数据时昵称有限制,对修改是否有限制
-----------------------------------------------------------------------------------------
5-1、修改2(课堂讲解,展示不同)
<input type="button" hehe="<%#Eval("Ids") %>" class="btn1" value="编辑" />
---- 主页,添加的是按钮。hehe为自定义元素,传值用。
<script type="text/javascript"> var btns = document.getElementsByClassName("btn1"); for (var i = 0; i < btns.length; i++) { btns[i].onclick = function () { window.open("Update.aspx?i=" + this.getAttribute('hehe'), "_blank", "toolbar=no menubar=no width=500 height=500"); } // 要传的值 打开新页面 页面样式 }
-- 后台数据处理一样