Hi,
this code works perfect (without the last line!). The elements of list
Any explanation for this: why is that line Session["time"] = time; not necessary?
Thanks
V
using System;
using System.Collections.Generic;
public partial class WebForm : System.Web.UI.Page
{
List
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Focus();
if (!Page.IsPostBack)
{
Session["time"] = time; // empty
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Label1.Text = "";
time = (List
time.Add(Convert.ToInt32(TextBox1.Text));
time.Sort();
for (int i=0;i< time.Count;i++)
Label1.Text += time[i].ToString() + " ";
TextBox1.Text = "";
//Session["time"] = time; // works without this line
}
}

Sachin SinghRank #66 · 26.2K reputationPosted Jan 7, 2023, 11:00 AM EST
put a debugger at below line
if (!Page.IsPostBack) { Session["time"] = time; // empty }if with each text change the flow is not entering inside the above curley braces (and setting session) (obviously it shouldn't enter for postbacks) then yes, it is strange .
Your session should be empty.
Valerie MeunierRank #1,526 · 1K reputationPosted Jan 7, 2023, 11:06 AM EST
Thanks for replying, but there must be a reason for that. No idea?
Sachin SinghRank #66 · 26.2K reputationPosted Jan 7, 2023, 11:11 AM EST
Are you sure if you remove time = (List)Session["time"]; then it stops working?
Valerie MeunierRank #1,526 · 1K reputationPosted Jan 7, 2023, 11:30 AM EST
if i do that, i see only the last added value.
Sachin SinghRank #66 · 26.2K reputationPosted Jan 7, 2023, 12:07 PM EST
if
if (!Page.IsPostBack)
{
Session["time"] = time; // empty
}
if the above line is not executing then there is no way session could be set.
Have you tested it with debugger?
Valerie MeunierRank #1,526 · 1K reputationPosted Jan 7, 2023, 1:16 PM EST
I tested it with a label inside the IF(): the first time, the label got a value, after that no. I'm sure it(s running properly:
if (!Page.IsPostBack)
{
Session["time"] = time; // empty
}
How can i test it with the debugger?
Valerie MeunierRank #1,526 · 1K reputationPosted Jan 7, 2023, 5:43 PM EST
May I suggest that you copy/paste this code and try yourself in VS. In aspx-file, you only need a label and a textbox.
This is the first line:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm.aspx.cs" Inherits="time.WebForm" %>
Thanks
Munib ButtRank #156 · 14.9K reputationPosted Jan 12, 2023, 4:08 PM EST
Somehow, the below is executed each time.
if (!Page.IsPostBack)
{
Session["time"] = time; // empty
}
Otherwise, the session cannot be set.
Valerie MeunierRank #1,526 · 1K reputationPosted Jan 13, 2023, 7:22 AM EST
I wonder why nobody has tried.
I did this as test:
if (!Page.IsPostBack)
{
Label2.Text += "not postback";
Session["time"] = time; // empty
}
else
{
Label2.Text += "postback";
}
And this gives not postback postback postback postback ...
This proves that it's executed only one time. Try yourself.