Hello, Team. I am stuck here at the finish-line on this program, I am trying to create a Click event handler for the Confirm button to save the “txtSales”, “lblDiscountAmount”, and “lblTotalPrice” in session state. Once the Confirm button is clicked it will take the user to the Confirmation page (page 2) where it will display what was inputted by the user on the first page.

Here is what I have on the Default.aspx page (just a snippet of the form)

<label>Sales price</label>       
        <asp:TextBox ID="txtSalesPrice" runat="server" CssClass="entry">100</asp:TextBox>
<label>Discount percent</label>
        <asp:TextBox ID="txtDiscountPercent" runat="server" CssClass="entry">20</asp:TextBox>
<label>Discount amount</label>
        <asp:Label ID="lblDiscountAmount" runat="server" CssClass="result"></asp:Label><br /><br />
<label>Total price</label>
        <asp:Label ID="lblTotalPrice" runat="server" CssClass="result" ></asp:Label><br /><br />
<asp:Button ID="btnCalculate" runat="server" Text="Calculate" 

OnClick="btnCalculate_Click" CssClass="button" />
        <asp:Button ID="Button1" runat="server" CssClass="button" Text="Confirm" PostBackUrl="~/Confirm.aspx" OnClick="Button1_Click" />

Here is the code behind for my Default.aspx for the Confirm button

protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Confirm.aspx?" + TextBox.txtSalesPrice);
        Response.Redirect("Confirm.aspx?") + Label.lblDiscountAmount);
        Response.Redirect("Confirm.aspx?") + label.lblTotalPrice);
    }

This is my Confirm.aspx page code snippet

<h1>Quotation confirmation</h1>
        <label>Sales price</label><asp:Label ID="lblSalesPrice" runat="server" CssClass="result"></asp:Label><br /><br />
        <label>Discount amount</label><asp:Label ID="lblDiscountAmount" runat="server" CssClass="result"></asp:Label><br /><br />
        <label>Total price</label><asp:Label ID="lblTotalPrice" runat="server" CssClass="result"></asp:Label><br />

This is my code behind for my Confirm.aspx

protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["SalesPrice"] && ["DiscountAmount"] && ["TotalPrice"] != null)
            lblSalesPrice.Text = ViewState["SalesPrice"].ToString();
            lblDiscountAmount.Text = ViewState["DiscountPrice"].ToString();
            lblTotalPrice.Text = ViewState["TotalPrice"].ToString();
    }
    protected void DisplayItems(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            TextBox txtSalesPrice = (TextBox)PreviousPage.FindControl("txtSalesPrice");
            lblSalesPrice.Text = txtSalesPrice.Text;
            TextBox txtDiscountAmount = (TextBox)PreviousPage.FindControl("txtDiscountAmount");
            lblDiscountAmount.Text = txtDiscountAmount.Text;
            TextBox txtTotalPrice = (TextBox)PreviousPage.FindControl("txtTotalPrice");
            lblTotalPrice.Text = txtTotalPrice.Text;
        }

    }

I have read my book and have done a lot of research online. I have even watched a couple YouTube videos. The problem is that it is still not working. I enjoy learning new languages and this is one of them. Yes, I am new to this but I should still have the logic down. Any help, someone, please.

Recommended Answers

All 3 Replies

So in the default code behind only line 3 is going to execute. Once you get to line 3 you are instructing the program to redirect to the confirm page.

Looks like you are trying to append a querystring parameter. If you wanted to pass the parameter via the URL, it would be something like this...

 txtSalesPrice.Text

But...you really want to validate/encode the data but that's another conversation.

If you want to store the values in session variables then you should be doing...

 Session["SalesPrice"] = txtSalesPrice.Text

Then access the data in the confirm page.

Refer to this documentation

Session State

Ok, so making a fews changes this is what I have

<asp:TextBox ID="txtSalesPrice" runat="server" CssClass="entry">100</asp:TextBox>

For my code behind on this page:

 protected void ConfirmButton_Click(object sender, EventArgs e)
    {

        Session["Sales"] = txtSalesPrice.Text;

        Session["Amt"] = lblDiscountAmount.Text;

        Session["Total"] = lblTotalPrice.Text;
        Response.Redirect("Confirm.aspx");
    }

...and for My Confirm page:

<label>Sales price</label><asp:Label ID="SESSION" runat="server" CssClass="result" ValidateRequestMode="Disabled">string txtSalesPrice = (string)(Session["Sales"]);</asp:Label><br /><br />
        <label>Discount amount</label><asp:Label ID="lblDiscountAmount" runat="server" CssClass="result">string lblDiscountAmount = (string)(Session["Amt"]);</asp:Label><br /><br />
        <label>Total price</label><asp:Label ID="lblTotalPrice" runat="server" CssClass="result">string lnlTotalPrice = (string)(Session["Total"]);</asp:Label><br />

But in reality, for my output I am getting the text = string txtSalesPrice = (string)(Session["Sales"]); Any thoughts on this? Thank you

Yes the problem is that on the confirmation page you should be accessing the session data from the code behind page.

So in your page load method...for example

 lblDiscountAmount.Text =  (string)(Session["Amt"]);
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.