Hi!
The code below has a simple task. However it does not manage to do the right thing. The whole point is that I want a session variable to save a string after I’ve clicked a button. And then after postback it should retain the value from the session variable and put it in a label control.
It does not work, I have to press the button twice to see the result I want. Is there something wrong with my code?
This is a dummy version of a cart that I am developing, with a generic List<T> which will reside inside a session variable that contains the names of certain controls that will be rendered after postback in the Page_Load event.
I’d gladly accept any help and/or suggestions!
The aspx-file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="setSession" Text="Button" />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
The aspx.cs-file
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["baba"] != null)
{
Label1.Text = Session["baba"].ToString();
}
}
protected void setSession(object sender, EventArgs e)
{
Session["baba"] = "Hi there!";
}
}