标签:
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Nation 的摘要说明 /// </summary> public class Nation { public Nation() { // // TODO: 在此处添加构造函数逻辑 // } public string NationName { get; set; } public string NationCode { get; set; } }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=Data0425;user=sa;pwd=123;"); cmd = conn.CreateCommand(); } /// <summary> /// 查询此用户是否存在,存在就返回true /// </summary> /// <param name="uname">用户名</param> /// <param name="upass">密码</param> /// <returns></returns> public bool SelectUser(string uname, string upass) { bool has = false; cmd.CommandText = "select *from Users where UserName=@a and PassWord=@b"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a", uname); cmd.Parameters.Add("@b", upass); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { has = true; } conn.Close(); return has; } /// <summary> /// 查询全部的Users表信息 /// </summary> /// <returns></returns> public List<Users> SelectAll() { List<Users> list = 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.UserName = dr[0].ToString(); u.PassWord = dr[1].ToString(); u.NickName = dr[2].ToString(); u.Sex = Convert.ToBoolean(dr[3]); u.Birthday = Convert.ToDateTime(dr[4]); u.Nation = dr[5].ToString(); list.Add(u); } } conn.Close(); return list; } public bool Delete(string name) { bool isok = false; cmd.CommandText = "delete from Users where username=‘" + name + "‘"; try { conn.Open(); cmd.ExecuteNonQuery(); isok = true; } catch { } finally { conn.Close(); } return isok; } public Users SelectUser(string name) { Users u = new Users(); cmd.CommandText = "select *from Users where username=@a"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a", name); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { dr.Read(); u.UserName = dr[0].ToString(); u.PassWord = dr[1].ToString(); u.NickName = dr[2].ToString(); u.Sex = Convert.ToBoolean(dr[3]); u.Birthday = Convert.ToDateTime(dr[4]); u.Nation = dr[5].ToString(); } conn.Close(); return u; } public bool UpdateUser(Users uuu) { bool isok = false; cmd.CommandText = "update users set password=@a,nickname=@b,sex=@c,birthday=@d,nation=@e where username=@aaaa"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a", uuu.PassWord); cmd.Parameters.Add("@b", uuu.NickName); cmd.Parameters.Add("@c", uuu.Sex); cmd.Parameters.Add("@d", uuu.Birthday); cmd.Parameters.Add("@e", uuu.Nation); cmd.Parameters.Add("@aaaa", uuu.UserName); try { conn.Open(); cmd.ExecuteNonQuery(); isok = true; } catch { } finally { conn.Close(); } return isok; } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Nation 的摘要说明 /// </summary> public class Nation { public Nation() { // // TODO: 在此处添加构造函数逻辑 // } public string NationName { get; set; } public string NationCode { get; set; } }
================================================================================================================================================
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; /// <summary> /// Users 的摘要说明 /// </summary> public class Users { SqlConnection conn = null; SqlCommand cmd = null; public Users() { conn = new SqlConnection("server=.;database=Data0425;user=sa;pwd=123;"); cmd = conn.CreateCommand(); } public string UserName { get; set; } public string PassWord { get; set; } public string NickName { get; set; } public bool Sex { get; set; } public DateTime Birthday { get; set; } public string Nation { get; set; } public string SexStr { get { return Sex ? "男" : "女"; } } public string NationName { get { string end = "<无>"; cmd.CommandText = "select *from Nation where NationCode=@a"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a", Nation); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { dr.Read(); end = dr["NationName"].ToString(); } conn.Close(); return end; } } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default1" %> <!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"> #tb1 { background-color: navy; width: 100%; text-align: center; } #tr_head { font-size: 18px; font-family: 微软雅黑; color: white; } #tr_head td { padding: 10px; } .item { background-color: white; } .item td { padding: 10px; } .alt_item { background-color: #e0e0e0; } .alt_item td { padding: 10px; } </style> </head> <body> <form id="form1" runat="server"> <h1>这是主界面</h1> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table id="tb1"> <tr id="tr_head"> <td>用户名</td> <td>密码</td> <td>昵称</td> <td>性别</td> <td>生日</td> <td>民族</td> <td>操作</td> </tr> </HeaderTemplate> <AlternatingItemTemplate> <tr class="alt_item"> <td><%#Eval("UserName") %></td> <td><%#Eval("PassWord") %></td> <td><%#Eval("NickName") %></td> <td><%#Eval("SexStr") %></td> <td><%#Eval("Birthday","{0:yyyy年MM月dd日}") %></td> <td><%#Eval("NationName") %></td> <td><a href="UpdatePage.aspx?aaa=<%#Eval("UserName") %>" target="_blank">修改</a> <a href="DeletePage.aspx?aaa=<%#Eval("UserName") %>">删除</a></td> </tr> </AlternatingItemTemplate> <ItemTemplate> <tr class="item"> <td><%#Eval("UserName") %></td> <td><%#Eval("PassWord") %></td> <td><%#Eval("NickName") %></td> <td><%#Eval("SexStr") %></td> <td><%#Eval("Birthday","{0:yyyy年MM月dd日}") %></td> <td><%#Eval("NationName") %></td> <td><a href="UpdatePage.aspx?aaa=<%#Eval("UserName") %>" target="_blank">修改</a> <a href="DeletePage.aspx?aaa=<%#Eval("UserName") %>">删除</a></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </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 Default1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Session["ok"] != null) { bool okok = Convert.ToBoolean(Session["ok"]); if (okok) { Response.Write("<script>alert(‘删除成功‘);</script>"); } else { Response.Write("<script>alert(‘删除失败‘);</script>"); } Session["ok"] = null; } Bind(); } public void Bind() { Repeater1.DataSource = new UsersData().SelectAll(); Repeater1.DataBind(); } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DeletePage.aspx.cs" Inherits="DeletePage" %> <!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> </div> </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 DeletePage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string Name = Request["aaa"]; bool ok=false; if (!String.IsNullOrEmpty(Name)) { ok = new UsersData().Delete(Name); } Session["ok"] = ok; Response.Redirect("Default1.aspx"); } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpdatePage.aspx.cs" Inherits="UpdatePage" %> <!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> <h1>用户修改</h1> 用户名:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br /><br /> 新密码:<asp:TextBox ID="TextBox1" TextMode="Password" runat="server"></asp:TextBox><br /><br /> 昵称:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /><br /> 性别:<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow"> <asp:ListItem Value="true">男</asp:ListItem> <asp:ListItem Value="false">女</asp:ListItem> </asp:RadioButtonList><br /><br /> 生日:<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>年<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>月<asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList>日<br /><br /> 民族:<asp:DropDownList ID="DropDownList4" runat="server"></asp:DropDownList><br /><br /> <asp:Button ID="Button1" runat="server" Text="确认修改" /> <asp:Button ID="Button2" runat="server" Text="关闭" /> </div> </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 UpdatePage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; //1、接收传过来的主键值 string Name = Request["aaa"]; //2、通过主键值将对象查出来 Users u = new UsersData().SelectUser(Name); //3、绑定控件的数据 if (IsPostBack == false) { BindDropDown(); BindDropDown1(); //4、将控件的默认值更改为对象的值 Label1.Text = u.UserName;//绑定用户名 TextBox2.Text = u.NickName;//绑定昵称 if (u.Sex)//绑定性别 { RadioButtonList1.SelectedIndex = 0; } else { RadioButtonList1.SelectedIndex = 1; } //绑定生日 DropDownList1.SelectedValue = u.Birthday.Year.ToString(); DropDownList2.SelectedValue = u.Birthday.Month.ToString(); DropDownList3.SelectedValue = u.Birthday.Day.ToString(); //绑定民族 DropDownList4.SelectedValue = u.Nation; } } void Button1_Click(object sender, EventArgs e) { Users uu = new Users(); uu.UserName = Label1.Text; uu.PassWord = TextBox1.Text; uu.NickName = TextBox2.Text.Trim(); uu.Sex = RadioButtonList1.Items[0].Selected; uu.Birthday = Convert.ToDateTime(DropDownList1.Text + "-" + DropDownList2.Text + "-" + DropDownList3.Text); uu.Nation = DropDownList4.SelectedItem.Value; bool okok = new UsersData().UpdateUser(uu); } public void BindDropDown() { for (int i = 1900; i <= DateTime.Now.Year; i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList1.Items.Add(li); } for (int i = 1; i <= 12; i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList2.Items.Add(li); } for (int i = 1; i <= 31; i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList3.Items.Add(li); } } public void BindDropDown1() { DropDownList4.DataSource = new NationData().SelectAll(); DropDownList4.DataTextField = "NationName"; DropDownList4.DataValueField = "NationCode"; DropDownList4.DataBind(); } }
标签:
原文地址:http://www.cnblogs.com/suiyuejinghao123/p/5690262.html