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);
}
}
}