How to store session information in ASP. net c #

Source: Internet
Author: User

Success!

In fact, I am only interested in winform technology, but now I have discovered that ASP. NET C # Web technology is becoming increasingly important!

As a result, I started to get started with what I know, especially some of the simplest things. I searched the internet for a long time. It's not too messy, that's not clear!

For example, if the session is okay, I don't know what the guys on the Internet are talking about?

Maybe I am too stupid! They really cannot understand their stuff!

 

So I decided to study it myself. It took a little time this afternoon and the results were as follows:

(1) In the load. aspx. CS file:

Public class myuser
{
Public myuser ()
{

}
Private string name;
Public string name
{
Get
{
Return name;
}
Set
{
Name = value;
}
}

}

// Note the class of the user object to which the myuser code logs in

// I wrote it in the load. aspx. CS file: but not in the load class, but in the same namespace

 

 

 

(2) In the load. aspx. CS file:

Protected void button#click (Object sender, eventargs E)
{

Try {
If (textbox1.text = "shigaofei" & textbox2.text = "123456") // here we use a hard verification to demonstrate that we will not connect to the database.
{
Myuser = new myuser ();
Myuser. Name = textbox1.text. Trim ();
This. session ["user"] = myuser; // Save the object


Server. Transfer ("loadsuccess. aspx ");
}
Else
{
Label1.text = "incorrect account or password ";
}

}

Catch

{

Label1.text = "An error occurred! ";

}


}

 

 

(3) In the loadsuccess. aspx. CS file:

 

Protected void page_load (Object sender, eventargs E)
{
Multiview1.activeviewindex = 0;
Label label1 = new label ();
Myuser my = new myuser ();
My = (myuser) session ["user"]; // retrieve the object in the session here, convert it to the myuser type, and assign it to the my object

Label1.text = "Welcome to here! "+ My. Name; // you can use my object to do things!

View1.controls. Add (label1 );

}

 

That's all.

This article is intended for beginners like me!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.