生活随笔
收集整理的這篇文章主要介紹了
web窗体的内置对象
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
內置對象: 1、Response - 響應請求對象 Response.Redirect("Default2.aspx"); //重定向 Response.Write("<script>window.open('Default2.aspx');</script>"); ---可以書寫任何東西,直接輸出出去 2、Request - 接收請求對象 Request["鍵"] - 放在等號右邊,用來取值 服務器控件 ?是根據ID來作為鍵 表單元素 ? ? 是根據name值來作為鍵 QueryString - 頁面地址拼接數據 如:Default2.aspx?aaa=呵呵&bbb=嘿嘿&ccc=hoho
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page
{ protected void Page_Load(
object sender, EventArgs e){Response.Write(Request[ " aaa " ]);}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Default1 : System.Web.UI.Page
{ protected void Page_Load(
object sender, EventArgs e){Button1.Click +=
Button1_Click;} void Button1_Click(
object sender, EventArgs e){Response.Redirect( " Default2.aspx?aaa= " +
TextBox1.Text); // Response.Write("呵呵");
}
} using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page
{ protected void Page_Load(
object sender, EventArgs e){TextBox1.Text =Request[
" aaa " ];}
}
1.后臺Response.Redirect("Default2.aspx?key="+值); 2.Form表單會通過submit按鈕自動提交數據,將數據提交給action設置的頁面中去,method=“get”方式來提交 method 提交方式 action 目標頁面 get提交方式
<% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default1.aspx.cs " Inherits = " Default1 " EnableViewStateMac = " false " %> <! DOCTYPE html > < html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" /> < title ></ title >
</ head >
< body > < form id ="form1" method ="get" action ="Default2.aspx" runat ="server" > < asp:TextBox ID ="User" runat ="server" ></ asp:TextBox >< br /> < asp:TextBox ID ="Pwd" runat ="server" ></ asp:TextBox >< br /> < asp:Button ID ="Button1" runat ="server" Text ="Button" /> </ form >
</ body >
</ html > using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Default1 : System.Web.UI.Page
{ protected void Page_Load(
object sender, EventArgs e){}} <% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default2.aspx.cs " Inherits = " Default2 " EnableViewStateMac = " false " %> <! DOCTYPE html > < html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" /> < title ></ title >
</ head >
< body > < form id ="form1" runat ="server" > < asp:TextBox ID ="TextBox1" runat ="server" ></ asp:TextBox > </ form >
</ body >
</ html > using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page
{ protected void Page_Load(
object sender, EventArgs e){TextBox1.Text =Request[
" User " ]+
" + " +Request[
" Pwd " ];}
}
post提交方式 - 直接數據提交方式 除了不在地址欄拼接數據,其它的沒有任何區別 =================================================================== 3、Session 就是一個臨時保存數據的對象,保存在服務器 可以理解為容器,變量
可以存Object類型,一個連接為獨立的一組,默認生命周期為20分鐘,如果在這20分鐘內重新進行數據提交或是頁面刷新,都會重置這20分鐘倒計時時間。
如果Session["鍵"]里面沒有內容的時候,使用時會拋出異常,所以一定要在使用session的值之前判斷此是否為空。
注意:Session不要過度使用,因為放的東西太大了,用戶連接多了就會導致內存溢出,服務器崩潰;也不要不使用,會造成資源浪費,因為內存中的數據是最快速的;
<% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default3.aspx.cs " Inherits = " Default3 " %> <! DOCTYPE html > < html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" > < meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" /> < title ></ title >
</ head >
< body > < form id ="form1" runat ="server" > 用戶名: < asp:TextBox ID ="TextBox1" runat ="server" ></ asp:TextBox >< br /> 密碼: < asp:TextBox ID ="TextBox2" runat ="server" TextMode ="Password" ></ asp:TextBox >< br /> < asp:Button ID ="Button1" runat ="server" Text ="登錄" />< asp:Label ID ="Label1" runat ="server" Text ="" ></ asp:Label > </ form >
</ html > using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Default3 : System.Web.UI.Page
{ protected void Page_Load(
object sender, EventArgs e){Button1.Click +=
Button1_Click;} void Button1_Click(
object sender, EventArgs e){ if (TextBox1.Text.Trim()==
" zhangsan " &&TextBox2.Text.Trim()==
" 123 " ){Session[ " username " ] =
TextBox1.Text;Response.Redirect( " Default4.aspx " );} else {Label1.Text =
" 用戶名密碼錯誤 " ;}}
} <% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default4.aspx.cs " Inherits = " Default4 " %> <! DOCTYPE html > < html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" /> < title ></ title >
</ head >
< body > < form id ="form1" runat ="server" > 這是session的主頁面 < br /> < asp:Label ID ="Label1" runat ="server" Text ="Label" ></ asp:Label > </ form >
</ body >
</ html > using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Default4 : System.Web.UI.Page
{ protected void Page_Load(
object sender, EventArgs e){ if (Session[
" username " ] !=
null ){Label1.Text = Session[
" username " ] +
" ,歡迎登錄! " ;} else {Response.Redirect( " Default3.aspx " );}}
}
4、cookie 臨時保存數據的一個對象,保存在客戶端 不要保存很重要的信息,因為會隨時被用戶刪掉,有些程序會自動抓取用戶電腦上的cookie信息,如果密碼保存進去了,就會被抓取到
賦值:使用Response對象 1、Response.Cookies.Add(new HttpCookie("鍵", 值));
2、Response.Cookies["鍵"].Value = 值;
3、制作持久Cookie : Response.Cookies["鍵"].Expires = DateTime.Now.AddDays(7);//設置7天后過期
取值:使用Request對象 Request.Cookies["UserName"].Value
<% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default5.aspx.cs " Inherits = " Default5 " %> <! DOCTYPE html > < html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" /> < title ></ title >
</ head >
< body > < form id ="form1" runat ="server" > 用戶名: < asp:TextBox ID ="TextBox1" runat ="server" ></ asp:TextBox >< br /> 密碼: < asp:TextBox ID ="TextBox2" runat ="server" ></ asp:TextBox >< br /> < asp:Button ID ="Button1" runat ="server" Text ="登錄" />< br /> < asp:CheckBox ID ="CheckBox1" runat ="server" Text ="7天自動登錄" /> < asp:Label ID ="Label1" runat ="server" Text ="" ></ asp:Label > </ form >
</ body >
</ html > using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Default5 : System.Web.UI.Page
{ protected void Page_Load(
object sender, EventArgs e){Button1.Click +=
Button1_Click;} void Button1_Click(
object sender, EventArgs e){ if (TextBox1.Text==
" zhangsan " &&TextBox2.Text==
" 123 " ){Response.Cookies[ " username " ].Value =
TextBox1.Text;Response.Redirect( " Default6.aspx " ); if (CheckBox1.Checked){Response.Cookies[ " username " ].Expires = DateTime.Now.AddDays(
7 );}} else {Label1.Text =
" 用戶名密碼錯誤! " ;}}
} <% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default6.aspx.cs " Inherits = " Default6 " %> <! DOCTYPE html > < html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" /> < title ></ title >
</ head >
< body > < form id ="form1" runat ="server" > 這是cookie的主頁面 < br /> < asp:Label ID ="Label1" runat ="server" Text ="" ></ asp:Label >< br /> < asp:Button ID ="Button1" runat ="server" Text ="退出" /> </ form >
</ body >
</ html > using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Default6 : System.Web.UI.Page
{ protected void Page_Load(
object sender, EventArgs e){Button1.Click +=
Button1_Click; if (Request.Cookies[
" username " ].Value!=
null ){Label1.Text = Request.Cookies[
" username " ].Value +
" ,歡迎登錄 " ;} else {Response.Redirect( " Default5.aspx " );}} void Button1_Click(
object sender, EventArgs e){Response.Cookies[ " username " ].Expires = DateTime.Now.AddDays(-
2 );Response.Redirect( " Default5.aspx " );}
}
5、Appliction 臨時保存數據的一個對象,保存在服務器端 全局變量 所有人訪問的都是同一個對象 賦值: Application["鍵"] = 值; 取值: Application.Get("鍵")
?
轉載于:https://www.cnblogs.com/fengsantianya/p/5681623.html
總結
以上是生活随笔 為你收集整理的web窗体的内置对象 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。