Daron_1 0 AntonioMicheal

Hello Everyone,

I am building multi-web application pages using the Default.aspx, Default.aspx.cs and Confirm.aspx and Confirm.aspx.cs. The problem that I am having is that when I click "Confirm" before clicking "Calculate," the event handler displays the "Click the Calculate button before the confirm" at the bottom of the page. It does that even if the Calculate button is clicked before the Confirm and also displays again when I reload the page. How can I get this to only display if the session state value of SalesPrice is null? Otherwise, it will redirect to the confirm page. Here is Default.aspx.cs:

 using System;
using System.Web;
using System.Web.UI;

using System.Web.UI.WebControls;

namespace XEx04Quotation
{

    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
        }

        protected void btnCalculate_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                decimal salesPrice = Convert.ToDecimal(txtSalesPrice.Text);

                // save the salesPrice into a session state variable 
                Session["SalesPrice"] = salesPrice;

                decimal discountPercent = Convert.ToDecimal(txtDiscountPercent.Text) / 100;

                decimal discountAmount = salesPrice * discountPercent;

                // save the discountAmount into a session state variable
                Session["Discount"] = discountAmount;

                decimal totalPrice = salesPrice - discountAmount;

                // save the totalPrice into a session state variable

                Session["TotalPrice"] = totalPrice;

                lblDiscountAmount.Text = discountAmount.ToString("c");
                lblTotalPrice.Text = totalPrice.ToString("c");
            }   
        }

        protected void Button1_Confirm(object sender, EventArgs e)
        {
             if(Session["SalesPrice"] != null)
            {
                Response.Redirect("Confirm.aspx");
            }
            else 
            {
                // This is the part I am concerned about
                lblmessage2.Text = "Click the Calculate button before you click confirm";
            }

        }
    }
}

Dani AI

Generated

— The symptom (the label message sticking around after a reload or showing even when Calculate was already clicked) strongly points to a stale label value surviving across postbacks/ViewState rather than a problem with the Session check itself. The simplest, robust fix is to explicitly clear or hide the message on every Page_Load and then only show it when Session["SalesPrice"] is null. Because Page_Load runs before your button handlers, that prevents old text from being rendered on the next request.

A minimal change that fixes the behavior:

protected void Page_Load(object sender, EventArgs e)
{
    UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;

    // remove any leftover message and only show it when SalesPrice is missing
    lblmessage2.Text = string.Empty;
    lblmessage2.Visible = (Session["SalesPrice"] == null);
}

Also clear the message after a successful calculation so the label can’t show later by accident:

// in btnCalculate_Click, after storing Session values
lblmessage2.Text = string.Empty;
lblmessage2.Visible = false;

In your Confirm click handler you can then set the message only when the session is missing (or hide it and redirect when present). Using Visible keeps the markup clean and avoids leaving text in ViewState. If this still happens, verify that the session is actually being set (add a quick debug/log or breakpoint to inspect Session["SalesPrice"]) and confirm your session mode/cookies aren’t causing the value to be lost between requests. Finally, consider using decimal.TryParse when reading the textbox to avoid exceptions that could abort the calculation and leave Session unset.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.