Reputation: 43
I have a simple form as:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
And code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
string myVariable;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
myVariable = "abc";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
}
At FormLoad, the myVariable is assigned to "abc".
At form, enter something to textbox and submit form. The myVariable's value is turned to "null"
Why my data at myVariable is lost? :(
I'm using: VS2008 SP1 Please help!
Upvotes: 1
Views: 9306
Reputation: 717
when you declare: string myVariable;
you declare it in the scope of the Page class. However, think about the process here.
1) You declare it with no value at the start of the Page class. 2) You check in Page_Load to see if it's a Postback. If no, give it a value, abc.
Now, when you submit the form, it IS a postback. Therefore, the assignment block in Page_Load doesn't run! So it is left as the same value it was when the Page was first loaded... which is NULL.
Make sense?
Try something like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
myVariable = "abc";
}
else
{
// hey im a postback, i need a value though!
myVariable = "xyz";
}
}
IMO I don't think state management is the nature of the question, as opposed to variable scope. If the OP doesn't understand scope, then state is a bit cart/horse problem.
Upvotes: 1
Reputation: 19862
A new instance of the Web page class is created each time the page is posted to the server. In traditional Web programming, this would typically mean that all information associated with the page and the controls on the page would be lost with each round trip. For example, if a user enters information into a text box, that information would be lost in the round trip from the browser or client device to the server.
Quoted from ASP .NET State Management. I thoroughly suggest you go through the State Management features of ASP .NET.
Also, all state management features aren't meant to store everything. This is another article, that will recommend which state management feature to use for what sort of situation.
Upvotes: 5
Reputation: 218837
Object instances aren't persistent across page loads. Think of each request made to the server as an entirely new instantiation of page classes. In order to persist data across multiple page requests, you need to store it outside the scope of the page class. You have a number of options:
But it needs to be outside the scope of the page's class, because the class is re-instantiated with each page request.
Upvotes: 1
Reputation: 108957
That is just the way it is(HTTP is stateless). You'll have to do
string myVariable;
protected void Page_Load(object sender, EventArgs e)
{
myVariable = "abc";
}
where you are initializing your variable everytime.
or save it to the Session collection.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["myVariable"] = "abc";
}
}
and use Session["myVariable"] as String
Upvotes: 1