Asp.Net
ASP.NET Web Forms Tutorial: User Controls Examples
In ASP.Net, it is possible to create re-usable code. The re-usable code can be used in many places...
The HTTP protocol on which all web applications work is a stateless protocol. By stateless, it just means that information is not retained from one request to another.
For instance, if you had a login page which has 2 textboxes, one for the name and the other for the password. When you click the Login button on that page, the application needs to ensure that the username and password get passed onto the next page.
In ASP.Net, this is done in a variety of ways. The first way is via a concept called ViewState. This is wherein ASP.Net automatically stores the contents of all the controls. It also ensures this is passed onto the next page. This is done via a property called the ViewState.
It is not ideal for a developer to change anything in the view state. This is because it should be handled by ASP.Net only.
The other way is to use an object called a "Session Object." The Session object is available throughout the lifecycle of the application. You can store any number of key-value pairs in the Session object. So on any page, you can store a value in the Session object via the below line of code.
Session["Key"]=value
This stores the value in a Session object and the 'key' part is used to give the value a name. This allows the value to be retrieved at a later point in time. To retrieve a value, you can simply issue the below statement.
Session["Key"]
In our example, we are going to use the Session object to store the name entered in the name textbox field in the page. We are then going to retrieve that value and display it on the page accordingly. Let's add the below code to the Demo.aspx.cs file.
protected void btnSubmit_Click(object sender,EventArgs e) { Session["Name"] = txtName.Text; Response.Write(Session["Name"]); lblName.Visible = false; txtName.Visible = false; 1stLocation.Visible = false; chkC.Visible = false; chkASP.Visible = false; rdMale.Visible = false; rdFemale.Visible = false; btnSubmit.Visible = false; }
Code Explanation:-
Once you make the above changes, you will see the following output
Output:
From the output, you can see that the Session value of name was retrieved and displayed in the browser.
Summary:
In ASP.Net, it is possible to create re-usable code. The re-usable code can be used in many places...
Download PDF 1) What is Web API? WebAPI is a framework which helps you to build/develop HTTP...
What is IIS? IIS or Internet Information Server is the server used to host .Net web applications. IIS is...
$20.20 $9.99 for today 4.6 (118 ratings) Key Highlights of ASP.NET Tutorial PDF 157+ pages eBook...
Let's look at an example of how we can implement a simple "hello world" application. For this, we...
What is ASP.Net? ASP.Net is a web development platform provided by Microsoft. It is used for...