Asp.Net C#中Session 如何儲存取資訊

來源:互聯網
上載者:User

http://www.cnblogs.com/xiaofengfeng/archive/2010/10/20/1857135.html因為剛剛接觸C#語言不久,於是我把控制台給扔了,因為我討厭控制台!

而事實上我只對WinForm技術感興趣,但事實上到現在我發現ASP.Net C#的Web技術是日顯重要起來!

於是開始接觸,剛剛起步於所知,尤其是一些最簡單的東西,到網上搜尋了很久,不是太亂就是說不清楚!

舉個例子來說,就說Session好了,不知道網上的那些傢伙到底在講什麼?

或許是我太笨吧!真是無法理解他們的東東!

 

於是我還是決定自己研究一下,今天下午花了一點時間,受到成果如下:

(1)Load.aspx.cs檔案中:

public class MYuser
    {
        public MYuser()
        {
           
        }
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
       
    }

//注意MYuser這個類代碼登陸的使用者物件的類

//我把它寫在了Load.aspx.cs檔案中:但不是Load類中,而只是同一命名空間

 

 

 

(2)Load.aspx.cs檔案中:

protected void Button1_Click(object sender, EventArgs e)
        {

            try{
                      if(TextBox1.Text=="shigaofei"&&TextBox2.Text=="123456")//這裡先用一硬校正示範一下就不串連資料庫了
                      {
                             MYuser myuser=new MYuser();
                             myuser.Name=TextBox1.Text.Trim();
                            this.Session["user"]=myuser;//把對象存進去

                              
                             Server.Transfer("LoadSuccess.aspx");
                       }
                       else
                       {
                            Label1.Text="帳號或密碼出現錯誤";
                       }

                  }

            catch

                   {

                         Label1.Text="發生錯誤!";

                   }

                       
          }

 

 

(3)LoadSuccess.aspx.cs檔案中:

 

protected void Page_Load(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
            Label label1 = new Label();
            MYuser my=new MYuser();
            my = (MYuser)Session["user"];//在這裡取出Session中的對象並轉化為MYuser類型,並將其賦值給my對象            

            label1.Text = "Welcome To Here!" + my.Name;//這裡就可以使用my對象做事情拉!

            View1.Controls.Add(label1);

        }

 

好了就這麼多

這篇文章希望對像我一樣的初學者有用!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.