ASP整合的材料.doc_第1页
ASP整合的材料.doc_第2页
ASP整合的材料.doc_第3页
ASP整合的材料.doc_第4页
ASP整合的材料.doc_第5页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

作业一1. 请新建网站创建课件中的两个页面。2. 请模仿代码中的Response.Write方法在以上两个页面最下方以任何形式在页面上显示自己的学号和姓名。 随机数:public partial class Default2 : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) int re = 0; Random ro = new Random(unchecked(int)DateTime.Now.Ticks); re = ro.Next(10,20); lb_RandNumber.Text = re.ToString(); Response.Write(090304039 陈桃香); 三角形:public partial class _Default : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) for (int i = 0; i 4; i+) Response.Write(ASP.NET2.0); Response.Write(090304039 陈桃香); Web.Config“Asp.Net 配置”选项。 设置和注释的完整列表在 ments 中,该文件通常位于 WindowsMicrosoft.NetFrameworkv2.xConfig 中-!- 通过 节可以配置 ASP.NET 使用的 安全身份验证模式, 以标识传入的用户。 -!- 如果在执行请求的过程中出现未处理的错误, 则通过 节可以配置相应的处理步骤。具体说来, 开发人员通过该节可以配置 要显示的 html 错误页 以代替错误堆栈跟踪。 -作业二1 新建网站如例ch2_1所示,并修改代码使得在晚上时能显示晚上好。NavigatePage.aspxusing System;using System.Data;using System.Configuration;using System.Collections;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 NavigatePage : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) /获取用户登陆名 this.lb_userName.Text = RequestUserName; /将系统时间与数据13进行比较,来获取问候语 int Time = DateTime.Now.Hour; string str; if (Time 18) str = 晚上!; else if (Time 14) str = 下午好!; else if (Time 12) str = 中午好!; else if (Time 6) str = 早上好!; else str = 凌晨好!; this.lb_pwd.Text = str; NavigatePage 无标题页 欢迎登陆本网站 2 新建网站如例ch2_2所示,(1)添加ASP.NET文件夹App_Code,在该文件夹下建立一个名为DBClass的类,将GetConnection方法移到该类中,使得所有页面类中的新建数据库连接对象全部调用DBClass类中的GetConnection方法完成;(2)将原先session中传递的UID改变为传递用户名;(3)原例中注册页面中没有判定是否己存在相同用户名,请改为需要预先判断才能插入;(4)原例注册成功没有任何提示,请修改为注册成功则转到item.aspx页面。Default.aspx.csusing System;using System.Data;using System.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;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e) protected SqlConnection GetConnection() /此方法用于创建数据库连接对象实例 string conn = ConfigurationManager.AppSettingsConnectionString.ToString(); /从Web.config中读取数据库连接字符串存入字符串变量conn中 /ConfigurationManager类用于提供对Webconfig的访问 /AppSettings为取节中的信息 SqlConnection myConn = new SqlConnection(conn); /实例化一个SqlConnection对象 return myConn; /返回数据库连接对象 protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) SqlConnection conn = GetConnection(); /获取一个数据库连接对象 string sqlStr; sqlStr = select * from Tb_Login where UseName= + Login1.UserName + and UsePassword= + Login1.Password + ; /形成查询用户名密码的sql语句 SqlDataAdapter adp = new SqlDataAdapter(sqlStr, conn); /建立一个sqlDataAdapter(sql数据适配器)对象并实例化,参数为sql语句及数据库连接对象 DataSet ds = new DataSet(); /建立一个DataSet(数据集)对象ds,用于存放从数据库中查询来的数据 adp.Fill(ds); /利用适配器adp填充数据集ds if (ds.Tables0.Rows.Count 0) /如果ds中第一个表格中行数大于0,即有查询结果 Response.Redirect(NavigatePage.aspx?UserName=+ Login1.UserName.ToString(); /重定向到NavigatePage页面,并传递用户名 else Response.Write(登录失败); /输出登录失败 adp.Dispose(); /释放 ds.Dispose(); Web“Asp.Net 配置”选项。 设置和注释的完整列表在 ments 中,该文件通常位于 WindowsMicrosoft.NetFrameworkv2.xConfig 中-!- 通过 节可以配置 ASP.NET 使用的 安全身份验证模式, 以标识传入的用户。 -!- 如果在执行请求的过程中出现未处理的错误, 则通过 节可以配置相应的处理步骤。具体说来, 开发人员通过该节可以配置 要显示的 html 错误页 以代替错误堆栈跟踪。 -作业三1 请使用response和session两种方法实现以下网站(请分别用两种方法制作出两个网站)default.aspxcompute.aspx要求:(1) default.aspx中,若输入的不是数字,则在页面上显示“请输入数字”。(2) compute.aspx中,能判断是否三角形,是则输出面积,不是则提示“这三条边长不能构成三角形,请返回重新输入”(3) 请在两个页面的下方显示客户端IP,在线人数,IP登录次数,总访问量,日访问量。(default.aspx中)答案:Default.aspx:Default.aspx.cs:using System;using System.Data;using System.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) if (!IsPostBack) int lastVisitCounter; if (Request.CookiesipVisitCounter = null) lastVisitCounter = 0; else lastVisitCounter = int.Parse(Request.CookiesipVisitCounter.Value); lastVisitCounter+; HttpCookie aCookie = new HttpCookie(ipVisitCounter); aCookie.Value = lastVisitCounter.ToString(); aCookie.Expires = DateTime.MaxValue; Response.Cookies.Add(aCookie); this.lb_IP.Text = Request.UserHostAddress; this.lb_Onlinecount.Text = Applicationonline_count.ToString(); this.lb_ipcount.Text = Request.CookiesipVisitCounter.Value.ToString(); this.lb_DayVisit.Text = Applicationdayvisit.ToString(); this.lb_TotalVisit.Text = Applicationtotalvisit.ToString(); protected void btn_Compute_Click(object sender, EventArgs e) double a, b, c; if (IsNumeric(this.tb_Oper1.Text.Trim() a = Convert.ToDouble(this.tb_Oper1.Text.Trim(); else Response.Write(请输入数字); return; if (IsNumeric(this.tb_Oper2.Text.Trim() b = Convert.ToDouble(this.tb_Oper2.Text.Trim(); else Response.Write(请输入数字); return; if (IsNumeric(this.tb_Oper3.Text.Trim() c = Convert.ToDouble(this.tb_Oper3.Text.Trim(); else Response.Write(请输入数字); return; Response.Redirect(compute.aspx?a= + a + &b= + b + &c= + c); public bool IsNumeric(string a) bool b = true; foreach (char c in a) b = char.IsNumber(c); if (b != true) return false; return true; Compute.aspx.cs:using System;using System.Data;using System.Configuration;using System.Collections;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 compute : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) double a = Convert.ToDouble(Requesta); double b = Convert.ToDouble(Requestb); double c = Convert.ToDouble(Requestc); if (a + b c & a + c b & b + c a) double p = (a + b + c) / 2; double area = Math.Sqrt(p * (p - a) * (p - c) * (p - b); Response.Write(该三角形的面积是 + area); else Response.Write(这不是一个三角形的三个边长); protected void lbtn_Back_Click(object sender, EventArgs e) Response.Redirect(default.aspx); Global: public static string totalvisits; public static string dayvisits; public static string day; void Application_Start(object sender, EventArgs e) / 在应用程序启动时运行的代码 Applicationonline_count = 0; /IIS启动时在线人数为0 Applicationdayvisit = 0; Applicationday = DateTime.Now.ToString(); /读取当前时间 int count = 0; StreamReader srd; /取得文件的实际路径 string file_path = Server.MapPath(visit.txt); /打开文件进行读取 srd = File.OpenText(file_path); while (srd.Peek() != -1) string strs = srd.ReadLine(); string str = strs.Split(,); count = int.Parse(str0); /读取总访问量 srd.Close(); object obj = count; /将从文件中读取的网站访问量存放在Application对象中 Applicationtotalvisit = obj; /总访问量存放在Application对象中 void Application_End(object sender, EventArgs e) / 在应用程序关闭时运行的代码,将总访问量+时间+日访问量保存到文件中 int Stat,Stat1 = 0; Stat = (int)Applicationdayvisit; Stat1 = (int)Applicationtotalvisit; /保存日期 string day0 = (string)Applicationday; string str = Stat1.ToString() + , + day0.ToString()+,+Stat.ToString(); / 将数据记录写入文件 string file_path = Server.MapPath(visit.txt); StreamWriter srw = new StreamWriter(file_path, false); srw.WriteLine(str); srw.Close(); void Application_Error(object sender, EventArgs e) / 在出现未处理的错误时运行的代码 void Session_Start(object sender, EventArgs e) / 在新会话启动时运行的代码 Application.Lock(); Applicationonline_count = 1 + (int)Applicationonline_count; /在线人数加1 int dayvisit,totalvisit; string NowDay,strs=null; /strs用于存放文本文件中的内容(总访问量+时间+日访问量) StreamReader srd; string file_path = Server.MapPath(visit.txt); /取得文件的实际路径 srd = File.OpenText(file_path); /打开文件进行读取 while (srd.Peek() != -1) strs = srd.ReadLine(); /保存从文件中读取的信息 srd.Close(); string str = strs.Split(,); /将读取的信息存放在字符串数组str中 totalvisit = Convert.ToInt32(str0); day = str1; /最近一次访问时间 dayvisit = Convert.ToInt32(str2); /日访问量 NowDay = DateTime.Now.ToString(); /文件中保存的时间值与系统时间相比,如果系统时间大,则重新开始计数 if (DateTime.Compare(Convert.ToDateTime(NowDay), Convert.ToDateTime(day) = 0) dayvisit = 0; /新的一天日访问量从0开始计算 day = DateTime.Now.AddDays(1).ToShortDateString() + + 00:00:00; /day保存下一天的开始时间 /最近一次访问时间 Applicationday = day; / 数据累加 Applicationdayvisit =+dayvisit; Applicationtotalvisit = +totalvisit; /保存日期 string day0 = (string)Applicationday; string str0 = totalvisit.ToString()+ , + day0.ToString() +,+ dayvisit.ToString(); / 将数据记录写入文件 StreamWriter srw1 = new StreamWriter(file_path, false); srw1.WriteLine(str0); srw1.Close(); Application.UnLock(); void Session_End(object sender, EventArgs e) / 在会话结束时运行的代码。 / 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 / InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer / 或 SQLServer,则不会引发该事件。 Application.Lock(); Applicationonline_count =(int)Applicationonline_count- 1; Application.UnLock(); 作业四(1)1 请将db_2数据库中tb_GoodsInfo的数据使用GridView显示在页面上,用户可以删除和修改记录。可修改的字段包括GoodsName、GoodsIntroduce、GoodsPrice、GoodsIsNew。请尝试实现多选删除,显示效果参见QQ邮箱中的多选删除。Web.config:DBClass.cs:using System;using System.Data;using System.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;using System.Data.SqlClient;public class DBClasspublic DBClass()/ TODO: 在此处添加构造函数逻辑/ public static SqlConnection GetConnection() string s = ConfigurationManager.AppSettingsConnectionString.ToString(); SqlConnection conn = new SqlConnection(s); return conn; Default.aspx:(GridView)Default.aspx.cs:using System;using System.Data;using System.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;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page SqlConnection conn = DBClass.GetConnection(); protected void Page_Load(object sender, EventArgs e) if (!IsPostBack) this.GV_Bind(); void GV_Bind() SqlDataAdapter adp = new SqlDataAdapter(select * from tb_goodsinfo, conn); DataSet ds = new DataSet(); adp.Fill(ds); this.GV_Goods.DataSource = ds.Tables0; this.GV_Goods.DataBind(); void DeleteGood(int goodsid) SqlCommand cmd = new SqlCommand(delete from tb_goodsinfo where goodsid= + goodsid, conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); void UpdateGood(int goodsid,string goodsname,string goodsintroduce,double goodsprice,string goodsisnew) SqlCommand cmd = new SqlCommand(update tb_goodsinfo set goodsname= + goodsname + ,goodsintroduce= + goodsintroduce + , goodsprice= + goodsprice + ,goodsisnew= + goodsisnew + where goodsid= + goodsid, conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); protected void GV_Goods_RowDeleting(object sender, GridViewDeleteEventArgs e) int goodsid = int.Parse(this.GV_Goods.DataKeyse.RowIndex.Value.ToString(); this.DeleteGood(goodsid); this.GV_Bind(); protected void GV_Goods_RowEditing(object sender, GridViewEditEventArgs e) this.GV_Goods.EditIndex = e.NewEditIndex; this.GV_Bind(); protected void GV_Goods_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) this.GV_Goods.EditIndex = -1; this.GV_Bind(); protected void GV_Goods_RowUpdating(object sender, GridViewUpdateEventArgs e) int goodsid = int.Parse(this.GV_Goods.DataKeyse.RowIndex.Value.ToString(); TextBox tb= (TextBox)this.GV_Goods.Rowse.RowIndex.Cells2.Controls0; string goodsname = tb.Text; tb = (TextBox)this.GV_Goods.Rowse.RowIndex.Cells3.Controls0; stri

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论