-3

这是我设置会话变量的页面之一,我将使用它来存储值

protected void confirmImageButton_Click(object sender, ImageClickEventArgs e)
{
    Session["confirmBooking"] = "confirm";
    Session["totalCost"] = toPayTextBox.Text;

    // If bachRadioButtonList SelectedValue != "Beach bach",
    // clear session variable, else set value to "Beach bach"
    Session["beachBach"] = (bachRadioButtonList.SelectedValue != "Beach bach");

    // If bachRadioButtonList SelectedValue != "Bush bach",
    // clear session variable, else set value to "Bush bach"
    Session["bushBach"] = (bachRadioButtonList.SelectedValue != "Bush bach");

    Response.Redirect("MainBookingform.aspx");
}

这是我提取这些会话变量的页面:

公共部分类 MainBookingform : System.Web.UI.Page

{

static int numberOfBeachBookingInteger = 0;

static int numberOfBushBookingInteger = 0;

static int totalRevenueInteger = 0;

公共部分类 MainBookingform : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
        bushBachLabel.Text = numberOfBushBookingInteger.ToString();

        if  ((Session["bushBach"] != null) && (Session["beachBach"] != null))
        {
            if (Session["beachBach"] != "confirm")
            {
                numberOfBeachBookingInteger += 1;
            }

            if (Session["bushBach"] != "confirm")
            {
                numberOfBushBookingInteger += 1;
            }

        }

    }
}

但是,当我调试程序时,它不会将 1 添加到变量会话: beachBach 和 bushBach 并且有时它不会添加任何值..

请帮忙

4

2 回答 2

0

这是您设置会话值的地方:

// will set value to true or false
Session["beachBach"] = (bachRadioButtonList.SelectedValue != "Beach bach");

// will set value to true or false
Session["bushBach"] = (bachRadioButtonList.SelectedValue != "Bush bach");

这是您检索它们的地方:

if (Session["beachBach"] != "confirm")  // will always fail because the session value is a boolean!

我怀疑你想要

if ((bool)(Session["beachBach"]) == true)  // the true is redundant but I add it here to be explicit
于 2013-08-27T01:43:01.260 回答
0

试试下面的代码

if (Session["confirmBooking"] != null && (string)Session["confirmBooking"] ==  "confirm" )
{
    if (Session["beachBach"] != null && (bool)(Session["beachBach"]) == true)
    {
        numberOfBeachBookingInteger += 1;
    }
    if (Session["bushBach"] != null && (bool)(Session["bushBach"]) == true)
    {
        numberOfBushBookingInteger += 1;
    }
}

似乎您还需要检查"confirm"会话变量以及beachBach,bushBach变量。

于 2013-08-27T03:10:10.337 回答