1
votes

I tried something by mistake and i do not understand why this is working:

I've created 2 asp.net web forms called page1.aspx and page2.aspx

On page1.aspx:

  • In code behind i declare a static string: field1.
  • I put a simple button.
  • When I click on this button:
    • field1="Hello world"
    • Response.Redirect("page2.aspx")

On page2.aspx, in Page_load, i display page1.field1 value. When page2.aspx is loaded, page1.aspx should not be loaded in memory. I do not understand why page1.field1 still contains "Hello world value !"

Can anyone explain me why this code works ? Is it a good thing to work this way ? Does asp store static fields in viewstate or session ?

Thanks

Here is page1.aspx:

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

<!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="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Here is page1.aspx.cs:

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

namespace WebApplication7
{
    public partial class page1 : System.Web.UI.Page
    {
        public static string field1;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            field1 = "Hello world";
            Response.Redirect("page2.aspx");
        }
    }
}

Here is page2.aspx.cs:

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

namespace WebApplication7
{
    public partial class page2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(page1.field1);
        }
    }
}
2

2 Answers

0
votes

The keyword "static" means that only one instance of a given variable exists for a class. Static variables are used to define constants because their values can be retrieved by invoking the class without creating an instance of it.

Static variables can be initialized outside the member function or class definition.

Note

Unlike other member variables, only one copy of the static variable exists in memory for all the objects of that class. Therefore, all objects share one copy of the static variable in memory.

In your situation, you had set field1 = "Hello World" in Page1 and on button click you will be redirected on Page2 and write that field1 variable on your page which is the static variable of your Page1 class.

0
votes

Once you make a field static, it will be in memory till app pool is recycled(full application memory is recycled). I don't know what you are trying, but declaring a static field in a code behind pages is not a good idea.