问题
I have the following test ASP page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
with the following code behind:
public partial class test : System.Web.UI.Page
{
int x = 0;
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = x.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
x++;
Label1.Text = x.ToString();
}
}
From a non web programming mindset I would have thought that int x
would have been a sort of object field for the test page class and that when i do x++
the value of x
would persist through postbacks but it seems this is not the case. So how exactly do these feilds work in terms of persistance? I find it odd also because when you click the button the Label1.Text
is changed to 1 the first time you click it but you can't get it past 1 after that.
Then also once I understand why this is happening what is the easiest way to make happen what I was originally wanting? (value of x
persists across postbacks)
This is what it looks like now using viewstate which is what i was looking for:
public partial class test : System.Web.UI.Page {
//static int x = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["x"] == null)
{
ViewState["x"] = 0;
Label1.Text = ViewState["x"].ToString();
}
//Label1.Text = x.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["x"] = Convert.ToInt32(ViewState["x"].ToString()) + 1;
Label1.Text = ViewState["x"].ToString();
//x++;
//Label1.Text = x.ToString();
}
}
回答1:
x
will not be persisted. Upon each request, x
will be initialised to 0.
Use, for example, session state to store the value of x
e.g. Session["x"] = x;
As for the label text, ASP.NET will store that inside of ViewState, which persists control values on each request. You can use ViewState yourself if you want to store a value that is only used on one page. Use Session if you want a value to be available on other pages in the site.
ViewState["x"] = x;
回答2:
If you want your variable x
to persist between each post back then store it in a session or cookie, and retrieve it back on each post back. Please don't use static
for it as posted by Samiey Mehdi as this will make it same for all the web sessions against the application domain.
You can store it in session like:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
Session["X"] = x;
int valueFromSession = Convert.ToInt32(Session["X"]);
Label1.Text = valueFromSession.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
int valueFromSession = Convert.ToInt32(Session["X"]);
valueFromSession++;
Session["X"] = valueFromSession;
Label1.Text = valueFromSession.ToString();
}
回答3:
like PHP, ASP can use both session variables and cookies to store persistent data. However, if you just use script variables, they will be reinitialized every time you run the script.
you can also use Static variables, they perform better, but I think there are security risks.
回答4:
As Jason Evans rightly pointed out.. you have to store the value in Session in order to persist. You could do the following:
protected void Page_Load(object sender, EventArgs e)
{
//Label1.Text = x.ToString();
Session["x"] = x;
}
protected void Button1_Click(object sender, EventArgs e)
{
x = Session["x"];
x++;
Label1.Text = x.ToString();
}
Ofcourse there are other ways of persisting values like Cookies, ViewState, adding Querystring etc. But among those, SessionState is the most commonly used.
来源:https://stackoverflow.com/questions/18409218/do-asp-page-variables-not-persist