如何将会话值从按钮单击传递到另一个CS文件

时间:2018-11-04 12:26:50

标签: c# asp.net

我试图尝试在一个asp.net表单(invoice.aspx)上单击一个按钮,然后将其传递给另一个响应(print.aspx)表单,并将该值插入标签中,以进行计算。但是,当我单击提交时,该值最终将为0。发生这种情况时,不会发生任何错误。可以做到吗?

下面的代码是:

print.aspx.cs

public partial class print : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

            DateTime date = DateTime.Now;
            lblDateIssue.Text = " Date Issue:" + date.ToShortDateString();

            var dueDate = date.AddDays(14);
            lblDueDate.Text = " DueDate:" + dueDate.ToShortDateString();

        double subTotal1 = (double)(Session["subTotal1"]);


        lblItem1Ttl.Text = subTotal1.ToString();
    }    
}

invoice.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    Session["subTotal1"] = 0.0;
}

public void btnSubmit_Click(object sender, EventArgs e)
{
    int qty1 = Int32.Parse(txtQty1.Text);
    double price1 = double.Parse(txtPrice1.Text);


    Session["subTotal1"] = (double)Session["subTotal1"] + (price1 * qty1);

}

}

任何指导将不胜感激

1 个答案:

答案 0 :(得分:0)

因此,不要在invoice.aspx表单中使用action属性。我删除了该属性,并放入Server.Transfer("print.aspx", true);,以将其发布到按钮单击事件的响应页面上。我还添加了try-catch函数来停止引发异常。因此,感谢@ dee-see,因为这是生命周期,只是必须采用稍微不同的方式来解决它。完整的解决方案如下

invoice.aspx.cs:

public partial class invoice : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            Session["subTotal1"] = 0.0;
            Session["subTotal2"] = 0.0;
            Session["subTotal3"] = 0.0;

        }

    }

    public void btnSubmit_Click(object sender, EventArgs e)
    {

        try
        {
            int qty1 = Int32.Parse(txtQty1.Text);
            double price1 = double.Parse(txtPrice1.Text);
            Session["subTotal1"] = (double)Session["subTotal1"] + (price1 * qty1);
        }
        catch
        {
            txtQty1.Text = "";
            txtPrice1.Text = "";
        }

        try
        { 
        int qty2 = Int32.Parse(txtQty2.Text);       
        double price2 = double.Parse(txtPrice2.Text);
        Session["subTotal2"] = (double)Session["subTotal2"] + (price2 * qty2);
        }
        catch
        {

        }

        try
        {
            int qty3 = Int32.Parse(txtQty3.Text);
            double price3 = double.Parse(txtPrice3.Text);
            Session["subTotal3"] = (double)Session["subTotal3"] + (price3 * qty3);

        }
        catch
        {
            txtQty3.Text = "";
            txtPrice3.Text = "";

        }

        Server.Transfer("print.aspx", true);
    }
}

print.aspx.cs:

public partial class print : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

            DateTime date = DateTime.Now;
            lblDateIssue.Text = " Date Issue:" + date.ToShortDateString();

            var dueDate = date.AddDays(14);
            lblDueDate.Text = " DueDate:" + dueDate.ToShortDateString();




        lblItem1Ttl.Text += "$" + (double)(Session["subTotal1"]);
        lblItem2Ttl.Text += "$" + (double)(Session["subTotal2"]);
        lblItem3Ttl.Text += "$" + (double)(Session["subTotal3"]);

        double total = ((double)(Session["subTotal1"]) + ((double)(Session["subTotal2"])) + (double)(Session["subTotal3"]));

        lblTotalAmount.Text += "$" + total;
    }


}