WebForm
Posted 酒不醉心
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebForm相关的知识,希望对你有一定的参考价值。
简单的人员管理系统
展示页面
添加人员
--判断添加人员的各种条件限制
-- 各种提示
修改人员信息
-- 人员原来信息绑定
--密码不显示,密码不改时用原来密码
人员删除
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 onclick="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以上是关于WebForm的主要内容,如果未能解决你的问题,请参考以下文章