Application State In ASP.NET Web Forms | State Management | ASP.NET Web Forms

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Application_State.WebForm1" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <style type="text/css">

    .auto-style1 {

        width:100%;

    }

    .auto-style2 {

        width: 93px;

    }

</style>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <table cellpadding="3" cellspacing="4" class="auto-style1">

    <tr>

        <td class="auto-style2">USERNAME</td>

        <td>

            <asp:TextBox ID="UserTextBox" runat="server" Width="202px"></asp:TextBox>

        </td>

    </tr>

    <tr>

        <td class="auto-style2">&nbsp;</td>

        <td>

            <asp:Button ID="SubmitButton" runat="server" Text="SUBMIT" OnClick="SubmitButton_Click"/>

        </td>

    </tr>

</table>

        </div>

    </form>

</body>

</html>

Above File is Application_State\WebForm1.aspx
















using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Application_State
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            Application["user"] = UserTextBox.Text;
            Response.Redirect("WebForm2.aspx");
        }
    }
}
Above File is Application_State\WebForm1.aspx.cs
















<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Application_State.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/WebForm3.aspx">GO TO PAGE 3</asp:LinkButton>
        </div>
    </form>
</body>
</html>
Above File is Application_State\WebForm2.aspx















using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Application_State
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Application["user"] != null)
            {
                //Generally we use "Response.Write()" when we want to display message at top left side in page
                Response.Write("Welcome " + Application["user"].ToString());
            }
            else
            {
                Response.Redirect("WebForm1.aspx");
            }
        }
    }
}
Above File is Application_State\WebForm2.aspx.cs

















<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="Application_State.WebForm3" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="LogoutButton" runat="server" Text="LOGOUT" OnClick="LogoutButton_Click"/>
        </div>
    </form>
</body>
</html>
Above File is Application_State\WebForm3.aspx















using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Application_State
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Application["user"] != null)
            {
                Response.Write("Welcome " + Application["user"].ToString());
            }
            else
            {
                Response.Redirect("WebForm1.aspx");
            }
        }

        protected void LogoutButton_Click(object sender, EventArgs e)
        {
            if (Application["user"] != null)
            {
                Application["user"] = null;
                Response.Redirect("WebForm1.aspx");
            }
        }
    }
}
Above File is Application_State\WebForm3.aspx.cs







Comments

Popular posts from this blog

Range Validator Control In ASP.NET Web forms | Form Validation | ASP.NET Web forms

Query String In ASP.NET Web Forms | State Management | ASP.NET | Web Forms

Validation Summary Control In ASP.NET Web forms | Form Validation | ASP.NET