web程式設計(4)….頁面間參數傳遞

來源:互聯網
上載者:User

一:用過的:

(1)通過URL連結地址傳遞 (加?..多個參數時加&para2=value中間不能有空格如href="receive.aspx?username=honge&pwd=123")

send.aspx:
        //或者頁面<a href="receive.aspx?username=honge"></a>
        protected void Button1_Click(object sender, EventArgs e)
        {     Request.Redirect("receive.aspx?username=honge"); }
receive.aspx:
            string username = Request.QueryString["username"];這樣可以得到參數值。
ps1:傳遞的參數預設都是string類型,所以不必加引號,也不必類型轉換後傳遞,在接收方為string類型.<漢字除外>

ps2:當傳遞多個參數時,用&隔開,注意不能有空格

ps3:當傳遞漢字時,常常先進行類型轉換, System.Web.HttpUtility.UrlEncode("你好!");

(2) 通過session
send.aspx:
        protected void Button1_Click(object sender, EventArgs e)
        { Session["username"] = "honge"; Request.Redirect("Default2.aspx"); }
receive.aspx:
         string username = Session["username"];     這樣可以得到參數值。

(3)通過cook:

send:
    Request.Cookies.Add(new HttpCookie("cookie_stop2_class","education"));
receive:
    HttpCookie myCook = Request.Cookies["cookie_stop2_class"];
    if(myCook!= null){
        string str =myCook.Value;  //str = education;

    }

 

 

(4)通過Application
send.aspx:
         protected void Button1_Click(object sender, EventArgs e)
         { Application["username"] = "honge"; Request.Redirect("Default2.aspx"); }
receive.aspx: string username = Application["username"];這樣可以得到參數值。

(5)通過Server.Transfer
send.aspx:
         public string Name { get { return "honge"; } }
         protected void Button1_Click(object sender, EventArgs e)
         { Server.Transfer("Default2.aspx"); }
 receive.aspx:
         send d = Context.Handler as send ;
         if (d != null) { Response.Write(d.Name);}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.