ASP.net Page pageload called in postback with IsPost back = false

ajaxcontroltoolkitasp.netispostbackpostbacksession-state

Hi everyone i have a strange problem which I hope u can help me with,
i have a normal Asp.net page in which i handle some state data in the page load like this

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["TempD"] = null;
            Session["Totals"] = null;
            //Handling Sessions here...
        }
    }

the problem is at a button post back the page_load gets called twice once with IsPostBack
= true which is ok , the second time however IsPostBack = false!!! which cause my code to enter the if condition and reset the state information which is not ok, i use some Ajax Toolkit controls in the page no update panels just some calenders and AutoCompletes.
here is the code for the button causing the post back

protected void TSBtnAddItem_Click(object sender, EventArgs e)
    {
        if (Session["TempD"] != null)
        {
            DataLayer.Invoicing.CInvoiceDetail InvoDetails = (DataLayer.Invoicing.CInvoiceDetail)Session["TempD"];
            DataLayer.Invoicing.CVarInvoiceDetail var = new DataLayer.Invoicing.CVarInvoiceDetail();
            if (LblCurrencyValue.Visible)
            {
                var.CurencyID = int.Parse(LblCurrencyValue.ToolTip);
            }
            else
            {
                var.CurencyID = int.Parse(CboCurrencyValue.SelectedValue);
            }
            var.ID = 0;
            var.InvoiceHeaderID = Convert.ToInt64(InvoiceHeaderID.Value);
            var.IsChanges = true;
            var.IsFreightItem = false;
            var.IsOption = true;
            var.ItemAmount = decimal.Parse(txtItemVal.Text);
            var.ItemName = CboItemName.SelectedItem.Text;
            var.ItemID = int.Parse(CboItemName.SelectedValue);
            var.Remarks = "";
            if (IsPartLoad.Checked == true)
            {
                ShipLink.Publics.ApplicationMethods.Item32 itm = LstCalcType.Find(delegate(ShipLink.Publics.ApplicationMethods.Item32 p1) { return Convert.ToInt32(p1.Name.Trim()) == var.ItemID; });
                if (itm == null)
                {
                    ADDToCalcList(Convert.ToString(var.ItemID));
                    if (NumUpDownPortRatio.Enabled == false)
                        var.ItemAmount = ChangeAmount(var.ItemID, var.ItemAmount);
                }
            }
            InvoDetails.lstCVarInvoiceDetail.Add(var);
            Session["TempD"] = InvoDetails;
            UGrdInvoiceDetailGrid.DataSource = InvoDetails.lstCVarInvoiceDetail;
            UGrdInvoiceDetailGrid.DataBind();
            CalcSalesTax();
            CalcDiscount();
            AddCaseUGrdInvoiceTotalGrid();

        }
    }

Best Answer

Have a look at this: What is the difference between Page.IsPostBack and Page.IsCallBack?

Integrate if (!IsCallBack) and you should be fine.

Related Topic