版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、.NET架构,主讲教师 张智 计算机学院,第6章 ASP.NET 内置对象,问题提出,Page2页面如何获取Page1的数据,页对象,ASP.NET页包含了很多用于页面请求处理的对象,开发人员通过调用这些对象及其属性可以实现页面相关业务的处理逻辑。,1 Request对象,示例,Page2页面如何获取Page1的数据,参考1,Default.aspx, ,Page2.aspx, ,提交数据的form使用 method=post 方法或者不指定method,参考2,Default.aspx, ,Page2.aspx, ,提交数据的form使用 method=get 方法,http:/localh
2、ost:49158/Page2.aspx?textbox1=12,补充,一次搞定get 和post 方法传送数据的代码: Request.ParamsTextBox1 或 RequestTextBox1,Request练习,从页面传2个整数到add.aspx相加。,程序参考,【返回】, ,=0)?+:)+b+=+(a+b); catch Response.Write(输入的整数过大); %,add.aspx,2 Response对象,示例,实现向浏览器输出“Hello”: Response.Write(Hello); 页面重定向到指定URL:Response.Redirect(); 实现向浏览
3、器输出login.txt文件: 假设login.txt:abcde Response.WriteFile(“login.txt”); /在页面上显示abcde,【返回】,3 Server对象,示例1,区别Execute和Transfer方法,Defalut.aspx.cs: protected void Page_Load(object sender, EventArgs e) Server.Execute(WebForm2.aspx); / Server.Transfer(WebForm2.aspx); Response.Write(这是Default.aspx); WebForm2.asp
4、x.cs: protected void Page_Load(object sender, EventArgs e) Response.Write(这是WebForm2.aspx!); ,Execute方法,Transfer方法,查阅资料,Response.Redirect()和Server.Transfer()区别,示例2问题提出,protected void Page_Load(object sender, EventArgs e) string str = Test String; Response.Write(str); ,Which one?,示例2,使用HtmlEncode和HTM
5、LEncode方法,protected void Page_Load(object sender, EventArgs e) string str = Test String; Response.Write(str); string strHtmlEncode = Server.HtmlEncode(str); Response.Write(strHtmlEncode + ); string strHtmlDecode = Server.HtmlDecode(strHtmlEncode); Response.Write(strHtmlDecode + ); ,strHtmlEncode内容:
6、,示例3,使用MapPath方法获取物理文件路径 Response.Write(Server.MapPath(Default.aspx); 区别: Response.Write(Request.PhysicalApplicationPath);,【返回】,C:UserszzllDocumentsVisual Studio 2008ProjectsWebApplication1WebApplication1Default.aspx,C:UserszzllDocumentsVisual Studio 2008ProjectsWebApplication1WebApplication1,4 Sess
7、ion对象,问题提出,浏 览 器,实现同一个客户端多页面间的数据共享/传递,直到客户关闭浏览器,Session概念,Session对象代表服务器与客户端所建立的会话. 从一个客户端打开浏览器并连接到服务器开始,到客户关闭浏览器离开这个服务器结束,被称为一个会话. 为什么需要Session? 当一个客户访问一个服务器时,可能会在这个服务器的多个页面之间反复跳转,服务器应当通过某种办法来识别这是来自同一个客户的不同请求,这种办法通常就是使用session对象。 session对象可以实现在一个会话期间的多页面间的数据共享/传递。,Defalut.aspx: protected void Page_
8、Load(object sender, EventArgs e) SessionSName = wustzz; Response.Redirect (WebForm1.aspx); WebForm1.aspx: protected void Page_Load(object sender, EventArgs e) SessionSName += 你好!; Server.Transfer(WebForm2.aspx); WebForm2.aspx: protected void Page_Load(object sender, EventArgs e) Response.Write(Sessi
9、onSName); ,示例1基本用法,如果不赋值,SessionSName默认为null,Session练习,输入一个=6的偶数,用2个素数和表示之。,自定义验证,参考,/自定义验证 protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) try int i = int.Parse(args.Value); if (i = 6 ,protected void Page_Load(object sender, EventArgs e) numDeco( int.Pars
10、e( (string)Sessiondata ) ); protected void numDeco(int num) for (int i = 2; i ; protected bool isPrime(int n) bool flag = true; for (int i = 2; i Math.Sqrt(n) + 1; i+) if (n % i = 0) flag = false; break; return flag; ,主要属性,示例2,Defalut.aspx: protected void Page_Load(object sender, EventArgs e) Sessio
11、nCount = 0; SessionName = tom; Server.Transfer(WebForm1.aspx); WebForm1.aspx: protected void Page_Load(object sender, EventArgs e) for (int i = 0; i ); ,运行情况,练习购物车,模拟一个购物车功能:2个商品购买页面(可互相切换),一个购物车(显示购物列表)。,Buy1.aspx,Buy2.aspx,Cart.aspx, 卖点别的 查看购物车 , 卖点别的 查看购物车 ,Buy1.aspx,Buy2.aspx,Buy1.aspx, 卖点别的 查看购
12、物车 ,protected void Button1_Click(object sender, EventArgs e) if (C1.Checked) Sessionb1 = C1.Text; else Sessionb1 = null; if (C2.Checked) Sessionb2 = C2.Text; else Sessionb2 = null; if (C3.Checked) Sessionb3 = C3.Text; else Sessionb3 = null; ,Buy2.aspx, 卖点别的 查看购物车 ,protected void Button1_Click(object
13、 sender, EventArgs e) if (C4.Checked) Sessionb4 = C4.Text; else Sessionb4 = null; if (C5.Checked) Sessionb5 = C5.Text; else Sessionb5 = null; if (C6.Checked) Sessionb6 = C6.Text; else Sessionb6 = null; ,cart.aspx, 您的购物车: ); % 返回 ,【返回】,5 Application对象,问题提出,浏 览 器,应用程序 供享数据,浏 览 器,如何在应用程序的用户之间传递信息直到关闭We
14、b服务器,示例1,页面访问量统计,protected void Page_Load(object sender, EventArgs e) if(ApplicationCount=null) ApplicationCount=1; else int n = (int)ApplicationCount; n+; ApplicationCount=n; Response.Write(页面访问量+ApplicationCount); ,示例2,页面访问量统计(图形化),使用Image控件?,参考,static string G(int counter) string myimage = ; int
15、n; while (counter 0) n = counter % 10; myimage = + myimage; counter = counter / 10; return myimage; protected void Page_Load(object sender, EventArgs e) if(ApplicationCount=null) ApplicationCount=1; else int n = (int)ApplicationCount; n+; ApplicationCount=n; Response.Write(页面访问量 ); Response.Write( G
16、(int)ApplicationCount) ); ,用Response输出HTML元素,示例3,页面访问量统计-使用Global.asax(全局应用程序类),Global.asax是一个文本文件,它提供全局可用代码。 这些代码包括应用程序的事件处理以及会话事件、方法和静态变量等。 每个应用程序在其根目录下只能有一个global.asax文件。,Global.asax片段, void Application_Start(object sender, EventArgs e) /在应用程序启动时运行的代码 void Application_End(object sender, EventArgs
17、 e) /在应用程序关闭时运行的代码 void Session_Start(object sender, EventArgs e) /在新会话启动时运行的代码 void Session_End(object sender, EventArgs e) /在会话结束时运行的代码。 ,代码,void Application_Start(object sender, EventArgs e) ApplicationCount = 0; void Session_Start(object sender, EventArgs e) Application.Lock(); /加锁 ApplicationCo
18、unt = (int)ApplicationCount + 1; Application.UnLock(); /解锁 ,【返回】,Cookie对象,Cookie是一种能够让Web服务器把少量数据储存到客户端的硬盘或内存,或是从客户端的硬盘读取数据的一种技术。 Cookie是当你浏览某网站时,由Web服务器置于你硬盘上的一个非常小的文本文件,它可以记录你的用户ID、密码、浏览过的网页、停留的时间等信息。 当你再次来到该网站时,网站通过读取Cookie,得知你的相关信息,就可以做出相应的动作,如在页面显示欢迎你的标语,或者让你不用输入ID、密码就直接登录等。,Win7 Cookie位置: C:Us
19、ersAdministratorAppDataRoamingMicrosoftWindowsCookies 文件名形如:administratorlocalhost1.txt,Cookie的限制,Cookie 的安全性问题与从客户端获取数据的安全性问题类似很容易被非法获取和利用。 用户可以将其浏览器设置为拒绝接受 Cookie。,大多数浏览器支持最大为 4096 字节的 Cookie。 浏览器限制站点可以在用户计算机上存储的 Cookie 的数量(如大多数浏览器只允许每个站点存储 20 个) 。,Cookie主要操作,1、写入Cookie示例: Response.CookiesuserName
20、.Value = wustzz; Response.CookiesuserName.Expires = DateTime.Now.AddDays(1); 2、读取Cookie示例: if( Request.CookiesuserName != null ) Label1.Text = Request.CookiesuserName.Value ;,到期日期,示例:页面访问量统计- Cookie方法,protected void Page_Load(object sender, EventArgs e) int counter; if ( Request.Cookiescounter = nul
21、l ) /读取Cookie counter = 0; else counter = int.Parse( Request.Cookiescounter.Value ); counter+; Response.Cookiescounter.Value = counter.ToString(); /写入Cookie Response.Cookiescounter.Expires = DateTime.Now.AddDays(1); Response.Write(页面访问量: + counter); ,读取Cookie集合,System.Text.StringBuilder output = new
22、 System.Text.StringBuilder(); HttpCookie aCookie; for(int i=0; i); output.Append(Cookie value = + Server.HtmlEncode(aCookie.Value) + ); Label1.Text = output.ToString();,删除Cookie 由于 Cookie 在用户的计算机中,因此无法将其直接移除。 但是,可以让浏览器来删除 Cookie。,思路: 创建一个与要删除的 Cookie 同名的新 Cookie,并将该 Cookie 的到期日期设置为早于当前日期的某个日期。 当浏览器检
23、查 Cookie 的到期日期时,会丢弃这个现已过期的 Cookie。,删除Cookie,HttpCookie aCookie; string cookieName; int limit = Request.Cookies.Count; for (int i=0; ilimit; i+) cookieName = Request.Cookiesi.Name; aCookie = new HttpCookie(cookieName); /另一种创建cookie方法 aCookie.Expires = DateTime.Now.AddDays(-1); Response.Cookies.Add(aC
24、ookie); /添加一个cookie ,删除所有可用 Cookie,思考,如何确定浏览器是否接受 Cookie? 思路:写一个 Cookie,然后再尝试读取该 Cookie。如果无法读取,则可以假定浏览器不接受 Cookie。,【完】,第一个页面写 Cookie,然后将浏览器重定向到第二个页面,protected void Page_Load(object sender, EventArgs e) if (!Page.IsPostBack) if (Request.QueryStringAcceptsCookies = null) Response.CookiesTestCookie.Value = ok; Response.CookiesTestCookie.Expires = DateTime.Now.AddMinutes(1); Response.Redirect(TestForCookies.aspx?redirect= + Server.UrlEncode(Request.Url.ToString(); else Label1.Text = Accept cookies = + Server.UrlEncode(Request.QueryStringAcceptsCookies); ,该页面首先测试
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 食品安全常识试题及答案
- 考研大数据综合试题及答案
- 2026年小升初学科测试题及答案
- 2026年分级护理测试题以及答案
- 2026年消防联动系统测试题及答案
- 2026年语文期中测试题库及答案
- 2026年医院管理知识测试题及答案
- 2026年全球最短智商测试题及答案
- 2026及未来5年中国一次性无菌针管行业发展研究报告
- 2026及未来5年中国五金产品行业发展研究报告
- DB33 642-2019 热电联产能效、能耗限额及计算方法
- 《冲突管理课件》课件
- 云南省公路工程试验检测费用指导价
- 2020初中物理自制教具-初中物理自制教具大全
- 加油站向周边商户风险告知书
- 中外城市建设史(全套课件595P)
- MotionView-MotionSolve应用技巧与实例分析
- 2023年1月浙江省普通高中学业水平考试地理试题及答案
- GB/T 9797-2022金属及其他无机覆盖层镍、镍+铬、铜+镍和铜+镍+铬电镀层
- GB/T 4437.1-2015铝及铝合金热挤压管第1部分:无缝圆管
- GB/T 15037-2006葡萄酒
评论
0/150
提交评论