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
}
}
Valerie MeunierPosted Jan 13, 2023, 12:22 PM
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.
Munib ButtPosted Jan 12, 2023, 9:08 PM
Somehow, the below is executed each time.
if (!Page.IsPostBack)
{
Session["time"] = time; // empty
}
Otherwise, the session cannot be set.
Valerie MeunierPosted Jan 7, 2023, 10:43 PM
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
Valerie MeunierPosted Jan 7, 2023, 6:16 PM
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?
Sachin SinghPosted Jan 7, 2023, 5:07 PM
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 MeunierPosted Jan 7, 2023, 4:30 PM
if i do that, i see only the last added value.
Sachin SinghPosted Jan 7, 2023, 4:11 PM
Are you sure if you remove time = (List)Session["time"]; then it stops working?
Valerie MeunierPosted Jan 7, 2023, 4:06 PM
Thanks for replying, but there must be a reason for that. No idea?
Sachin SinghPosted Jan 7, 2023, 4:00 PM
put a debugger at below line
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.