文章出處

     這么說吧,當程序啟動時,ASP.NET會自動創建一些經常使用的類的實例,而這些實例就是ASP.NET的內置對象,常用的實例對象有:Response(來自HttpResponse類)、Request(來自HttpRequest類)、Server(來自HttpServerUtility類)、Application、Session、Context等,這些對象下又可以調很多方法。所謂界面重定向,就是跳轉界面,發送一URL請求,有時候順帶傳遞點參數啥的。當完成一種要求時如果有多個解決方案,說明這個‘要求’分為好幾種情況,各個方案也是各有利弊,無萬全之策。下面介紹下重定向的幾種常用方法。

 QueryString:

     最常用的傳遞方式,可高效傳遞數字或文本值,導航欄會顯示參數值,所以缺乏安全性,可傳遞對象

 Session:

     導航欄不會顯示參數,可傳對象,大小不限,缺點是占用資源多

     用例:用戶第一次訪問時,將定義的Session變量存儲在服務器端,當再次訪問時可以直接從Session對象中的集合獲取相關數據,多為論壇、購物車、網站后臺管理登陸系統使用,

 Application:

      導航欄不會顯示參數,可傳對象,占用資源少

 Cookie:

      導航欄不會顯示參數,由于傳遞的數據大小被大多數瀏覽器限制為4096byte,所以用來存儲用戶ID之類的標識符

A界面:發送

        //QueryString
        protected void BtnQuery_Click(object sender, EventArgs e)
        {
            Response.Redirect("B.aspx?name2="+TextBox3.Text.Trim()+"&"+"name4="+TextBox4.Text.Trim());
        }
        //Session
        protected void BtnSess_Click(object sender, EventArgs e)
        {
            //Session.Contents.Add("name3",TextBox3.Text);
            //Session.Add("name3",TextBox3.Text);
            Session["name3"] = TextBox3.Text;
            Session["name4"] = TextBox4.Text;
            Response.Redirect("B.aspx?");
        }
        //Application
        protected void BtnApplic_Click(object sender, EventArgs e)
        {
            Application["name3"] = TextBox3.Text;
            Application["name4"] = TextBox4.Text;
            Response.Redirect("B.aspx?");
        }  
       //Cookie 
        protected void BtnCook_Click(object sender, EventArgs e)
        {
            HttpCookie name1 = new HttpCookie("name3");
            HttpCookie name2 = new HttpCookie("name4");
            name1.Value = TextBox3.Text;
            name2.Value = TextBox4.Text;
            Response.AppendCookie(name1);
            Response.AppendCookie(name2);
            Response.Redirect("B.aspx?");
        }

 

B界面:接收

        //QueryString
        protected void BtnQuGet_Click(object sender, EventArgs e)
        {
            TextBox3.Text = Request.QueryString["name3"];
            TextBox4.Text = Request.QueryString["name4"];
           
            
        }
        //Session
        protected void BtnSessGet_Click(object sender, EventArgs e)
        {
            TextBox3.Text = Session["name3"].ToString();
            TextBox4.Text = Session["name4"].ToString();
        }
        //Application
        protected void btnAppli_Click(object sender, EventArgs e)
        {
            TextBox3.Text = Application["name3"].ToString();
            TextBox4.Text = Application["name4"].ToString();
        
        }
        //Cookie
        protected void BtnCook_Click(object sender, EventArgs e)
        {
            TextBox3.Text = Request.Cookies["name3"].Value.ToString();
            TextBox4.Text = Request.Cookies["name4"].Value.ToString();
        }

當然,以上是最簡單最基礎的重定向Demo,實際上,僅僅是介紹Cookie,就需要很多篇幅說明,Session也是一樣,相對來說簡單點

情景: A登錄界面驗證成功

處理: A界面重定向之前加一句:  Session["name1"] = username; 來傳遞參數

跳轉到B目的界面之后,在Page_Load方法中寫入判斷:

        protected void Page_Load(object sender, EventArgs e)
        {
            string user = string.Empty;
            if ( Session["name1"]==null)
            {
                user = "";
            }
            else
            {
                user = Session["name1"].ToString();
            }
            if (user=="")
            {
                Response.Write("<script language='javascript'>alert('未登錄無法查看此界面');location.href='Login.aspx'</script>");
            }

            Label1.Text = user;
        }

這樣的話即使你關閉界面,再直接訪問目的頁,也可以成功訪問,一般Session時間默認值是20min,只要不超過這個時間就可以訪問

 

參考書籍:ASP.NET基礎教程

 

----------市人皆大笑,舉手揶揄之


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()