views:

458

answers:

3
protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        lblPostBack.Text = " Text created first time";
    }
    else
    {
        if (Session["Counter"] == null)
        {
            lblPostBack.Text = "PostBack x however strange becasue if is postback it's mean somebody clicked button and session value has been created";

        }
        else
        {
            lblPostBack.Text = "PostBack x should be count here";
        }
    }
}
protected void cmd_Click(object sender, EventArgs e)
{
    int _counter;
    if (Session["Counter"] == null)
    {
        _counter = 1;

    }
    else
    {
        _counter = (int)Session["Counter"] + 1;
    }
    Session["Counter"] = _counter;
    lblPostBack.Text += "Counter: " + _counter.ToString();
}
A: 

Ok it works, just FF mess up

Rob
No, I have still problem, I need to click twice in order to have right message. Somebody knows why?
Rob
A: 

I have added following method and works fine.

private int _counter;

protected void Page_Load(object sender, EventArgs e)
{
(...)

protected void Page_PreRender(Object sender, EventArgs e) { Session["Counter"] = _counter; }

Rob
+1  A: 

Assuming this is ASP.NET: It's because the Click event on your button fires after the Load event on your page, so the session has not been set.

MSDN on the page lifecycle might be good reading - the button click is a "postback event" in the table in that document.

If I've got the wrong end of the stick, please explain what messages you get after the button clicks, and what you were expecting. Some framework and language tags on the question might not go amiss, either.

Dave Roberts

related questions