数据库的链接:
using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Web; /// <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 denglu where [email protected] and [email protected]"; 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 denglu"; 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; } /// <summary> /// 删除 /// </summary> /// <param name="name"></param> /// <returns></returns> public bool Delete(string name) { bool isok = false; cmd.CommandText = "delete from denglu where username=‘" + name + "‘"; try { conn.Open(); cmd.ExecuteNonQuery(); isok = true; } catch { } finally { conn.Close(); } return isok; } /// <summary> /// 条件查询全部内容 /// </summary> /// <param name="name"></param> /// <returns></returns> public Users SelectUser(string name) { Users u = new Users(); cmd.CommandText = "select *from denglu where [email protected]"; 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; } /// <summary> /// 修改 /// </summary> /// <param name="uuu"></param> /// <returns></returns> public bool UpdateUser(Users uuu) { bool isok = false; cmd.CommandText = "update denglu set [email protected],[email protected],[email protected],[email protected],[email protected] where [email protected]"; 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; } /// <summary> /// 添加 /// </summary> /// <param name="l"></param> /// <returns></returns> public bool insertuser(Users l) { bool isok = false; cmd.CommandText = "insert into users values (@a,@b,@c,@d,@e,@f)"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a", l.UserName); cmd.Parameters.Add("@b", l.PassWord); cmd.Parameters.Add("@c", l.NickName); cmd.Parameters.Add("@d", l.Sex); cmd.Parameters.Add("@e", l.Birthday); cmd.Parameters.Add("@f", l.Nation); conn.Open(); cmd.ExecuteNonQuery(); isok = true; conn.Close(); return isok; } }
Nation数据库
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.Data.SqlClient; using System.Linq; using System.Web; /// <summary> /// NationData 的摘要说明 /// </summary> public class NationData { SqlConnection conn = null; SqlCommand cmd = null; public NationData() { conn = new SqlConnection("server=.;database=Data0425;user=sa;pwd=123;"); cmd = conn.CreateCommand(); } public List<Nation> SelectAll() { List<Nation> list = new List<Nation>(); cmd.CommandText = "select *from minzu"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Nation n = new Nation(); n.NationCode = dr["nationcode"].ToString(); n.NationName = dr["nationname"].ToString(); list.Add(n); } } conn.Close(); return list; } }
一、程序制作的步骤:
1、想想是要实现什么功能,把界面搭出来
2、想想在哪里写代码
3、补齐一些小问题
如果这3步也解决不了问题,那么问题出在你就不知道这个程序是要干什么
二、用户体验问题:
让用户简便的操作 - 站在使用者的角度来考虑程序的设计
去网上找一些类似的程序来看看他们是如何设计的,或者是模板
三、控件数据的加载:
例如:DropDownList里面添加年月日,不需要手动添加,可是使用循环
public void LoadDate(int start, int end, DropDownList dd) { for (int i = start; i <= end; i++) { ListItem li = new ListItem(); li.Text = i.ToString(); li.Value = i.ToString(); dd.Items.Add(li); } }
protected void Page_Load(object sender, EventArgs e) { if (IsPostBack == false) { LoadDate(1900, DateTime.Now.Year, DropDownList1); LoadDate(1, 12, DropDownList2); LoadDate(1, 31, DropDownList3); foreach (ListItem li in DropDownList1.Items) { if (li.Text == "1980") { li.Selected = true; } } }
数据库数据加载:
List<Nation> NaList = context.Nation.ToList();//将Nation表中的数据全部取出来变成一个泛型集合 //foreach (Nation na in NaList) //{ // ListItem li = new ListItem(na.NationName, na.NationCode); // DropDownList4.Items.Add(li); //} DropDownList4.DataSource = NaList; DropDownList4.DataTextField = "NationName"; DropDownList4.DataValueField = "NationCode"; DropDownList4.DataBind();
四、事件的委托写法:
优点:清晰,易于查找
控件名.事件名 += (按两下tab键,会自动创建事件出来)
Button1.Click += Button1_Click;//提交按钮事件
五、三级联动:
AutoPostBack - 自动提交事件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!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"> .aaa { width:100px; height:30px; font-size:25px; } </style> </head> <body> <form id="form1" runat="server"> <asp:DropDownList ID="DropDownList1" runat="server" CssClass="aaa" AutoPostBack="True"></asp:DropDownList> <asp:DropDownList ID="DropDownList2" runat="server" CssClass="aaa" AutoPostBack="True"></asp:DropDownList> <asp:DropDownList ID="DropDownList3" runat="server" CssClass="aaa"></asp:DropDownList> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </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 Default2 : System.Web.UI.Page { DataClasses2DataContext context = new DataClasses2DataContext();//LinQ操作类 List<ChinaStates> list = new List<ChinaStates>(); protected void Page_Load(object sender, EventArgs e)//页面加载事件 { DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged;//省的下拉列表选项更改事件 DropDownList2.SelectedIndexChanged += DropDownList2_SelectedIndexChanged; list = context.ChinaStates.ToList();//将全部的地区数据查询出来 if (IsPostBack == false) { #region 绑定省数据 List<ChinaStates> sheng = list.Where(r => r.ParentAreaCode == "0001").ToList(); DropDownList1.DataSource = sheng; DropDownList1.DataTextField = "AreaName"; DropDownList1.DataValueField = "AreaCode"; DropDownList1.DataBind(); #endregion #region 绑定市数据 string shengCode = DropDownList1.SelectedItem.Value; List<ChinaStates> shi = list.Where(r => r.ParentAreaCode == shengCode).ToList(); DropDownList2.DataSource = shi; DropDownList2.DataTextField = "AreaName"; DropDownList2.DataValueField = "AreaCode"; DropDownList2.DataBind(); #endregion List<ChinaStates> quxian = list.Where(r => r.ParentAreaCode == DropDownList2.SelectedItem.Value).ToList(); DropDownList3.DataSource = quxian; DropDownList3.DataTextField = "AreaName"; DropDownList3.DataValueField = "AreaCode"; DropDownList3.DataBind(); } } void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) { List<ChinaStates> quxian = list.Where(r => r.ParentAreaCode == DropDownList2.SelectedItem.Value).ToList(); DropDownList3.DataSource = quxian; DropDownList3.DataTextField = "AreaName"; DropDownList3.DataValueField = "AreaCode"; DropDownList3.DataBind(); } //省选项改变事件 void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string shengCode = DropDownList1.SelectedItem.Value; List<ChinaStates> shi = list.Where(r => r.ParentAreaCode == shengCode).ToList(); DropDownList2.DataSource = shi; DropDownList2.DataTextField = "AreaName"; DropDownList2.DataValueField = "AreaCode"; DropDownList2.DataBind(); List<ChinaStates> quxian = list.Where(r => r.ParentAreaCode == DropDownList2.SelectedItem.Value).ToList(); DropDownList3.DataSource = quxian; DropDownList3.DataTextField = "AreaName"; DropDownList3.DataValueField = "AreaCode"; DropDownList3.DataBind(); } }
六、Repeater灵活使用:
不要认为Repeater只能制作table表格,它叫重复器,可以重复的制作任何东西
七、JS知识补充:
1、用JS来制作超链接按钮 - window.open();
2、如果要把JS代码写在head里面,那么需要加window.onload事件
注意:
JS的方法不能写在onload事件中
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %> <!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> <script type="text/javascript"> window.onload = function () { var oBtn1 = document.getElementById(‘btn1‘); oBtn1.onclick = function () { window.open(‘Default1.aspx‘, ‘_blank‘); }; }; function bbb() { alert(‘bbb‘); } </script> </head> <body> <form id="form1" runat="server"> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" /><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <a href="Default1.aspx" target="_blank">注册</a> <input type="button" id="btn1" value="注册"/> <input type="button" value="呵呵" onclick="bbb();" /> </form> </body> </html>
protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Response.Redirect("Default1.aspx"); } protected void Button2_Click(object sender, EventArgs e) { Label1.Text = "123"; } }
时间: 2024-09-30 13:04:35