ASP实验报告.docx
《ASP实验报告.docx》由会员分享,可在线阅读,更多相关《ASP实验报告.docx(29页珍藏版)》请在冰豆网上搜索。
ASP实验报告
实验一数据源操作
一设计要求
使用ExecuteReader()方法实行数据的模糊查询,用户在文本框中输入要查询记录的“姓名”或“部门”后单击“提交按钮”,在GridView控件中将显示查询结果。
单击“显示全部”按钮将显示所有记录。
GridView任务菜单中能够实现数据的删除、编辑、选择及记录的分页显示。
二程序界面设计
三实现该程序功能的代码如下
usingSystem.Data.SqlClient;
publicpartialclass_Default:
System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
this.Title="ExecuteReader()方法使用示例";
txtKeyword.Focus();
}
protectedvoidbtn_Click(objectsender,EventArgse)
{
stringstrSQL;
Buttonbtn=(Button)sender;
if(btn.ID=="btnSubmit")
{
if(txtKeyword.Text=="")
{
Response.Write("alert('查询关键字不能为空!
');");
return;
}
strSQL="select*fromemployeewhere"+rbtnField.SelectedItem.Text+"like'%"+txtKeyword.Text+"%'";
}
else
{
txtKeyword.Text="";
strSQL="select*fromemployee";
}
SqlConnectionconn=newSqlConnection();
conn.ConnectionString="DataSource=PC-20091216ATDB/SQLEXPRESS;InitialCatalog=employee;IntegratedSecurity=True";
conn.Open();
SqlCommandcom=newSqlCommand(strSQL,conn);
SqlDataReaderdr=com.ExecuteReader();
GridView1.DataSource=dr;
GridView1.DataBind();
if(GridView1.Rows.Count==0)
{
Response.Write("alert('未找到符合记录的条件!
');");
}
conn.Close();
}
}
四程序运行界面结果
当输入“教务处”查询时,执行结果如下:
当点击“编辑”时,执行结果如下:
实验二用户登陆
一设计一个简单的用户登录程序。
解:
根据题意设计,初始的登录界面如下图1,登录后进入如下图2的界面:
图1
图2
登录系统的程序代码如下:
publicpartialclassDefault2:
System.Web.UI.Page
{
structLoginUser
{
publicstringUsername;
publicstringPassword;
}
LoginUser[]userinfo=newLoginUser[3];
protectedvoidPage_Load(objectsender,EventArgse)
{
this.Title="欢迎使用登录系统";
txtUsername.Focus();
userinfo[0].Username="张三";userinfo[0].Password="1234567";
userinfo[1].Username="李四";userinfo[1].Password="2345678";
userinfo[2].Username="王五";userinfo[2].Password="3456789";
}
protectedvoidvalxUsername_ServerValidate(objectsource,ServerValidateEventArgsargs)
{
stringstrName=txtUsername.Text.Trim();
intn=strName.Length;
args.IsValid=true;
if(txtUsername.Text=="")
{
valxUsername.Text="用户名不能为空!
";
args.IsValid=false;
return;
}
}
protectedvoidvalxPassword_ServerValidate(objectsource,ServerValidateEventArgsargs)
{
stringstrPassword=txtPassword.Text.Trim();
args.IsValid=true;
if(strPassword.Length<6||strPassword.Length>12)
{
valxPassword.Text="密码长度必须在6~12之间!
";
args.IsValid=false;
}
}
protectedvoidbtnOk_Click(objectsender,EventArgse)
{
if(!
Page.IsValid)
{
Response.Write("alert('用户输入数据未通过验证!
');");
return;
}
else
{
for(inti=0;i<3;i++)
{
if(txtUsername.Text==userinfo[i].Username&&txtPassword.Text==userinfo[i].Password)
{
Session["username"]=userinfo[i].Username;
Response.Redirect("denglu.aspx");
}
}
}
}
}
员工信息查询系统的代码如下:
publicpartialclass_Default:
System.Web.UI.Page
{
structEmployee
{
publicstringname;
publicboolsex;
publicstringbirthday;
publicstringnative;
publicstringeducation;
publicstringuniversity;
}
Employee[]emp=newEmployee[7];
protectedvoidPage_Load(objectsender,EventArgse)
{
this.Title="欢迎使用员工信息查询系统";
lblMsg.Visible=false;
txtName.Focus();
emp[1].name="张三";emp[1].sex=false;emp[1].birthday="1976.2";
emp[1].native="南京";emp[1].education="本科";emp[1].university="南京大学";
emp[2].name="李四";emp[2].sex=false;emp[2].birthday="1978.6";
emp[2].native="天津";emp[2].education="本科";emp[2].university="天津大学";
emp[3].name="王五";emp[3].sex=false;emp[3].birthday="1980.5";
emp[3].native="北京";emp[3].education="硕士";emp[3].university="武汉大学";
emp[4].name="赵六";emp[4].sex=false;emp[4].birthday="1972.2";
emp[4].native="郑州";emp[4].education="博士";emp[4].university="南开大学";
emp[5].name="陈七";emp[5].sex=false;emp[5].birthday="1982.7";
emp[5].native="济南";emp[5].education="本科";emp[5].university="同济大学";
emp[6].name="刘八";emp[6].sex=false;emp[6].birthday="1974.8";
emp[6].native="云南";emp[6].education="硕士";emp[6].university="河南大学";
}
protectedvoidbtnOK_Click(objectsender,EventArgse)
{
lblName.Text="";
lblSex.Text="";
lblBirthday.Text="";
lblNative.Text="";
lblEdu.Text="";
lblUniversity.Text="";
intiNum=0;
if(txtName.Text=="")
{
lblMsg.Visible=true;
lblMsg.Text="请输入姓名!
";
return;
}
for(inti=1;i<=6;i++)
{
if(emp[i].name==txtName.Text)
{
iNum=i;
break;
}
}
if(iNum==0)
{
lblMsg.Visible=true;
lblMsg.Text="查无此人!
";
return;
}
lblName.Text=emp[iNum].name;
lblSex.Text=emp[iNum].sex?
"男":
"女";
lblBirthday.Text=emp[iNum].birthday;
lblNative.Text=emp[iNum].native;
lblEdu.Text=emp[iNum].education;
lblUniversity.Text=emp[iNum].university;
}
}
实验三页面访问次数
一设计要求
使用Session对象设计一个站点计数器。
要求将来访人数存放在站点内的coounter.txt文件内,该数字不会因服务器或网站重新启动而丢失。
程序运行时要求将当前绘画的ID显示到页面中,注意比较SessionID值的变化情况。
二程序设计要点
1本题需要使用读写文本文件故需要将语句“usingSystem.IO;”添加代码窗口最上方的命名空间引用声明区。
2使用Windows附件的记事本程序创建一个名为counter.txt的文本文件,该文件用于存放访问量数字,创建文件时可在其中写入初始值0。
并将文件保存到站点文件夹内。
三实现该程序的代码
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.IO;
publicpartialclassmain:
System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
this.Title="使用session对象设计网站计数器";
stringstrPaht=Server.MapPath("counter.txt");
StreamReadersr=newStreamReader(strPaht);
intiNum=int.Parse(sr.ReadLine());
sr.Close();
if((string)(Session["counter"])!
="null")
{
Session["counter"]="";
iNum=iNum+1;
StreamWritersw=newStreamWriter(strPaht);
sw.WriteLine(iNum);
sw.Close();
}
Response.Write("
当前sessionID值为:
"+Session.SessionID+"
");
Response.Write("
你是本站第"+iNum.ToString()+"位访问者
");
}
}
四程序运行界面
实验四留言板
一设计要求
设计一个网上留言板,具有登陆和留言的功能。
二程序界面设计
(1)登陆页面设计
(2)留言板页面设计
(3)用户注册页面设计
(4)恢复遗忘密码功能页面设计
三实现该程序的代码
(1)登陆程序代码
usingSystem.Data.OleDb;
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
publicpartialclass_Default:
System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
this.Title="简易留言板-用户登陆";
}
protectedvoidbtnLogin_Click(objectsender,EventArgse)
{
if(txtUsername.Text==""||txtPassword.Text=="")
{
Response.Write("alert('用户名或密码不得为空!
');");
return;
}
OleDbConnectionconn=newOleDbConnection();
conn.ConnectionString="PROVIDER=mICROSOFT.jET.oLEdB.4.0;"+"dATAsOURCE="+sERCER.mapPath("App_Data/msg.mdb");
conn.Open();
stringstrSecPwd=FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text,"MD5");
stringstrSQL="select*fromuserinfowhere用户名='"+txtUsername.Text+"'and密码='"+strSecPwd+"'";
OleDbCommdcom=newOleDbCommand(strSQL,conn);
OleDbDataReaderdr=com.ExecuteReader();
if(!
dr.Read())
{
Response.Write("alter('用户名或密码错误!
');");
}
else
{
Session["pass"]=dr["拥护名"];
Response.Redirect("msg.aspx");
}
dr.Close();
conn.Close();
}
protectedvoidbtnRegister_Click(objectsender,EventArgse)
{
Response.Readirect("register.aspx");
}
protectedvoidbtnRepassword_Click(objectsender,EventArgse)
{
if(txtUsername.Text=="")
{
Response.Write("alert('请输入用户名!
');");
}
else
{
Session["username"]=txtUsername.Text;
Response.Redirect("recover.aspc");
}
}
protectedvoidtxtPassword_TextChanged(objectsender,EventArgse)
{
}
}
(2)留言板程序代码
publicpartialclassmsg:
System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
this.Title="查看和发表留言";
if(Session["pass"]==null)
{
Response.Redirect("default.aspx");
}
if(!
IsPostBack)
{
SqlConnectionconn=newSqlConnection();
conn.ConnectionString="server=.;database=msg;uid=admin;password=654321";
stringstrSel="select*frommessageorderby时间desc";
SqlCommandselcom=newSqlCommand(strSel,conn);
SqlDataAdapterda=newSqlDataAdapter();
da.SelectCommand=selcom;
DataSetds=newDataSet();
da.Fill(ds,"massage");
GridView1.DataSource=ds.Tables["message"].DefaultView;
GridView1.DataBind();
}
lblUser.Text=Session["pass"].ToString();
}
protectedvoidbtnSubmit_Click(objectsender,EventArgse)
{
if(txtMsg.Text=="")
{
Response.Write("alert('留言内容不得为空!
');");
return;
}
SqlConnectionconn=newSqlConnection();
conn.ConnectionString="server=.;database=msg;uid=admin;password=654321";
stringstrVal="'"+lblUser.Text+"','"+txtMsg.Text+"','"+System.DateTime.Now.ToString()+"'";
stringstrIns="insertintomassage(留言人,内容,时间)Values("+strVal+")";
SqlCommandinsCom=newSqlCommand(strIns,conn);
SqlDataAdapterda=newSqlDataAdapter();
conn.Open();
da.InsertCommand=insCom;
da.InsertCommand.ExecuteNonQuery();
stringstrSel="select*frommessageorderby时间";
SqlCommandselcom=newSqlCommand(strSel,conn);
da.SelectCommand=selcom;
DataSetds=newDataSet();
da.Fill(ds,"message");
GridView1.DataSource=ds.Tables["massage"].DefaultView;
GridView1.DataBind();
txtMsg.Text="";
conn.Close();
}
protectedvoidbtnCancel_Click(objectsender,EventArgse)
{
Session.Clear();