ASP 中常用代码
Cookie 的用法: 的用法:
//Cookie 的用法 HttpCookie cookie = new HttpCookie("first"); //第一个 Cookie
cookie.Expires = DateTime.Now.AddDays(30); //设置 Cookie 的消失时间, 时间为 30 天 cookie.Values["name"] = "lsc"; //name <-->lsc 为键值对 cookie.Values["password"] = "12345"; Response.Cookies.Add(cookie); //将指定的 Cookie 保存到本机 //设置 Cookie 的值
//获取 Cookie 的值 HttpCookie c = (HttpCookie)Request.Cookies["first"]; Response.Write(c.Values["name"] + "
"); Response.Write(c.Values["password"]);
Application
写在 Gloal.asax
的用法: 的用法:
voidApplication_Start(object sender, EventArgs e) { // Code that runs on application startup //服务器启动时运行的代码 //关于计数器的使用 //在此初始化计数器 Application["count"] = 0; //当服务器启动时调用,该网站访问数为 0
开始 } 写在页面端 protected void Page_Load(object sender, EventArgs e) { try { //取出 application 中的值| Application.Lock(); int count = (int)Application["count"]; count++; Application["count"] = count; //将此返回服务?器 Response.Write("你是第+Application["count"] + "位访客¨a"); Application.UnLock(); } catch (Exception ex) { Response.Write(“
”); } } 如果写在页面端,则用户刷新一次,访问人数就增加
voidSession_Start(object sender, EventArgs e) { // Code that runs when a new session is started //在新会话启动时运行的代码 //取出 application 中的值 Application.Lock(); int count = (int)Application["count"]; count++; Application["count"] = count; //将此返回服务器 Response.Write("你是第" + Application["count"] + "位访客"); Application.UnLock(); }
将此写在 Global.asax 中使得页面只有通过重新访问,访问数才能增加,否则不能 增加 Lock()和 UnLock()的意义是必须是一个加完后,另外一个在加
Response 的用法: 的用法:
Response 用于向页面输出内容和实现页面的重定向 Response.Write(); Resposne.Redirect("网址") Response.Cookies.Add(); 用于添加 Cookie
Request 的用法: 的用法:
Request 用于获取表单的值 String name=request.queryString["name"]; String name=request.from["name"]; String name=request.params["name"]; --最
常用的一种方法 String name=request["name"];
Session 的用法:(Session 中可以传递各种数据类型的数据也可以传递 的用法:
类) Session 用于在各个页面之间传递数据 首先是将值存放在 Session 中: Session["name"] = name; Session["password"] = pass
word; 然后获取值: string name = (string)Session["name"]; string password = (string)Session["password"];
###在实际
工作中,Session 传递的值一般是一个封装类
JavaScript 中页面自动跳转的方法: 中页面自动跳转的方法:
将其添加在 ASP.NET 中即可实现 ASP.NET 中的自动跳转 在 ASP.
NET 中设置密码框即在 TextBox 中添加 TextMode="password" ASP.NET 中自身的 TextBox 设置密码框添加 TextMode 属性 ---利用
的数据绑定: 关于 DropDownList 的数据绑定:
首先在 DropDownList 中的事件 SelectedIndexChanged 中添加: this.Image1.ImageUrl = this.DropDownList1.SelectedValue.ToString(); 1、利用图形界面进行数据绑定,详见数据绑定视频 2、利用 ArrayList 容器进行数据绑定,但是只能是 5 条一下的数据 "img/" +
if (!IsPostBack) { ArrayList list = new ArrayList(); list.Add("1.jpg"); list.Add("2