1、ASP实验报告实验一 数据源操作一 设计要求 使用ExecuteReader()方法实行数据的模糊查询,用户在文本框中输入要查询记录的“姓名”或“部门”后单击“提交按钮”,在GridView控件中将显示查询结果。单击“显示全部”按钮将显示所有记录。GridView任务菜单中能够实现数据的删除、编辑、选择及记录的分页显示。二 程序界面设计三 实现该程序功能的代码如下 using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page protected void Page_Load(object sende
2、r, EventArgs e) this.Title = ExecuteReader()方法使用示例; txtKeyword.Focus(); protected void btn_Click(object sender, EventArgs e) string strSQL; Button btn = (Button)sender; if (btn.ID = btnSubmit) if (txtKeyword.Text = ) Response.Write(alert(查询关键字不能为空!);); return; strSQL = select*from employee where + r
3、btnField.SelectedItem.Text + like% + txtKeyword.Text + %; else txtKeyword.Text = ; strSQL = select*from employee; SqlConnection conn = new SqlConnection(); conn.ConnectionString = Data Source=PC-20091216ATDB/SQLEXPRESS;Initial Catalog=employee;Integrated Security=True; conn.Open(); SqlCommand com =
4、new SqlCommand(strSQL, conn); SqlDataReader dr = com.ExecuteReader(); GridView1.DataSource = dr; GridView1.DataBind(); if (GridView1.Rows.Count = 0) Response.Write(alert(未找到符合记录的条件!);); conn.Close(); 四 程序运行界面结果 当输入“教务处”查询时,执行结果如下:当点击“编辑”时,执行结果如下:实验二 用户登陆一设计一个简单的用户登录程序。 解:根据题意设计,初始的登录界面如下图1,登录后进入如下图2
5、 的界面: 图1图2登录系统的程序代码如下:public partial class Default2 : System.Web.UI.Page struct LoginUser public string Username; public string Password; LoginUser userinfo = new LoginUser3; protected void Page_Load(object sender, EventArgs e) this.Title = 欢迎使用登录系统; txtUsername.Focus(); userinfo0.Username = 张三; use
6、rinfo0.Password = 1234567; userinfo1.Username = 李四; userinfo1.Password = 2345678; userinfo2.Username = 王五; userinfo2.Password = 3456789; protected void valxUsername_ServerValidate(object source, ServerValidateEventArgs args) string strName = txtUsername.Text.Trim(); int n = strName.Length; args.IsVa
7、lid = true; if (txtUsername.Text = ) valxUsername.Text = 用户名不能为空!; args.IsValid = false; return; protected void valxPassword_ServerValidate(object source, ServerValidateEventArgs args) string strPassword = txtPassword.Text.Trim(); args.IsValid = true; if (strPassword.Length 12) valxPassword.Text = 密
8、码长度必须在612之间!; args.IsValid = false; protected void btnOk_Click(object sender, EventArgs e) if (!Page.IsValid) Response.Write(alert(用户输入数据未通过验证!);); return; else for (int i = 0; i 3; i+) if (txtUsername.Text = userinfoi.Username & txtPassword.Text = userinfoi.Password) Sessionusername = userinfoi.Use
9、rname; Response.Redirect(denglu.aspx); 员工信息查询系统的代码如下:public partial class _Default : System.Web.UI.Page struct Employee public string name; public bool sex; public string birthday; public string native; public string education; public string university; Employee emp = new Employee7; protected void P
10、age_Load(object sender, EventArgs e) this.Title = 欢迎使用员工信息查询系统; lblMsg.Visible = false; txtName.Focus(); emp1.name = 张三; emp1.sex = false; emp1.birthday = 1976.2; emp1.native = 南京; emp1.education = 本科; emp1.university = 南京大学; emp2.name = 李四; emp2.sex = false; emp2.birthday = 1978.6; emp2.native = 天津
11、; emp2.education = 本科; emp2.university = 天津大学; emp3.name = 王五; emp3.sex = false; emp3.birthday = 1980.5; emp3.native = 北京; emp3.education = 硕士; emp3.university = 武汉大学; emp4.name = 赵六; emp4.sex = false; emp4.birthday = 1972.2; emp4.native = 郑州; emp4.education = 博士; emp4.university = 南开大学; emp5.name =
12、 陈七; emp5.sex = false; emp5.birthday = 1982.7; emp5.native = 济南; emp5.education = 本科; emp5.university = 同济大学; emp6.name = 刘八; emp6.sex = false; emp6.birthday = 1974.8; emp6.native = 云南; emp6.education = 硕士; emp6.university = 河南大学; protected void btnOK_Click(object sender, EventArgs e) lblName.Text =
13、 ; lblSex.Text = ; lblBirthday.Text = ; lblNative.Text = ; lblEdu.Text = ; lblUniversity.Text = ; int iNum = 0; if (txtName.Text = ) lblMsg.Visible = true; lblMsg.Text = 请输入姓名!; return; for (int i = 1; i = 6; i+) if (empi.name = txtName.Text) iNum = i; break; if (iNum = 0) lblMsg.Visible = true; lbl
14、Msg.Text = 查无此人!; return; lblName.Text = empiNum.name; lblSex.Text = empiNum.sex ? 男 : 女; lblBirthday.Text = empiNum.birthday; lblNative.Text = empiNum.native; lblEdu.Text = empiNum.education; lblUniversity.Text = empiNum.university; 实验三 页面访问次数一 设计要求使用Session对象设计一个站点计数器。要求将来访人数存放在站点内的coounter.txt文件内
15、,该数字不会因服务器或网站重新启动而丢失。程序运行时要求将当前绘画的ID显示到页面中,注意比较SessionID值的变化情况。二 程序设计要点 1 本题需要使用读写文本文件故需要将语句“using System.IO;”添加代码窗口最上方的命名空间引用声明区。2 使用Windows附件的记事本程序创建一个名为counter.txt的文本文件,该文件用于存放访问量数字,创建文件时可在其中写入初始值0。并将文件保存到站点文件夹内。三 实现该程序的代码using System;using System.Data;using System.Configuration;using System.Coll
16、ections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.IO;public partial class main : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) this.Ti
17、tle = 使用session对象设计网站计数器; string strPaht = Server.MapPath(counter.txt); StreamReader sr = new StreamReader(strPaht); int iNum = int.Parse(sr.ReadLine(); sr.Close(); if (string)(Sessioncounter) != null) Sessioncounter = ; iNum = iNum + 1; StreamWriter sw = new StreamWriter(strPaht); sw.WriteLine(iNum
18、); sw.Close(); Response.Write(当前sessionID值为: + Session.SessionID + ); Response.Write(你是本站第 + iNum.ToString() + 位访问者); 四 程序运行界面实验四 留言板一 设计要求 设计一个网上留言板,具有登陆和留言的功能。二 程序界面设计(1)登陆页面设计(2)留言板页面设计(3)用户注册页面设计(4)恢复遗忘密码功能页面设计三 实现该程序的代码(1)登陆程序代码using System.Data.OleDb;using System;using System.Data;using System
19、.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) this.Title=简
20、易留言板用户登陆; protected void btnLogin_Click(object sender, EventArgs e) if (txtUsername.Text = | txtPassword.Text = ) Response.Write(alert(用户名或密码不得为空!);); return; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = PROVIDER=mICROSOFT.jET.oLEdB.4.0; + dATA sOURCE= + sERCER.mapPath(App_D
21、ata/msg.mdb); conn.Open(); string strSecPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, MD5); string strSQL=select*from userinfo where 用户名=+ txtUsername.Text+and密码=+strSecPwd+; OleDbCommd com = new OleDbCommand(strSQL, conn); OleDbDataReader dr = com.ExecuteReader(); i
22、f (!dr.Read() Response.Write(alter(用户名或密码错误!);); else Sessionpass=dr拥护名; Response.Redirect(msg.aspx); dr.Close(); conn.Close();protected void btnRegister_Click(object sender, EventArgs e) Response.Readirect(register.aspx);protected void btnRepassword_Click(object sender, EventArgs e) if (txtUsername
23、.Text = ) Response.Write(alert(请输入用户名!);); else Sessionusername=txtUsername.Text; Response.Redirect(recover.aspc); protected void txtPassword_TextChanged(object sender, EventArgs e)(2)留言板程序代码public partial class msg : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) this.Title
24、 = 查看和发表留言; if (Sessionpass = null) Response.Redirect(default.aspx); if (!IsPostBack) SqlConnection conn = new SqlConnection(); conn.ConnectionString = server=.;database=msg;uid=admin;password=654321; string strSel = select * from message order by 时间 desc; SqlCommand selcom = new SqlCommand(strSel,c
25、onn); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = selcom; DataSet ds = new DataSet(); da.Fill(ds,massage); GridView1.DataSource = ds.Tablesmessage.DefaultView; GridView1.DataBind(); lblUser.Text = Sessionpass.ToString(); protected void btnSubmit_Click(object sender, EventArgs e) if
26、(txtMsg.Text = ) Response.Write(alert(留言内容不得为空!);); return; SqlConnection conn = new SqlConnection(); conn.ConnectionString = server=.;database=msg;uid=admin;password=654321; string strVal = + lblUser.Text + , + txtMsg.Text + , + System.DateTime.Now.ToString() + ; string strIns = insert into massage
27、(留言人,内容,时间) Values(+strVal+); SqlCommand insCom = new SqlCommand(strIns,conn); SqlDataAdapter da = new SqlDataAdapter(); conn.Open(); da.InsertCommand = insCom; da.InsertCommand.ExecuteNonQuery(); string strSel = select*from message order by 时间; SqlCommand selcom = new SqlCommand(strSel,conn); da.SelectCommand = selcom; DataSet ds = new DataSet(); da.Fill(ds, message); GridView1.DataSource = ds.Tablesmassage.DefaultView; GridView1.DataBind(); txtMsg.Text = ; conn.Close(); protected void btnCancel_Click(object sender, EventArgs e) Session.Clear();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1