会话变量错误

时间:2013-08-27 01:34:23

标签: c# asp.net

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

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");
}

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

public partial class MainBookingform:System.Web.UI.Page

{

static int numberOfBeachBookingInteger = 0;

static int numberOfBushBookingInteger = 0;

static int totalRevenueInteger = 0;

public partial class 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,有时它不会添加任何值..

请帮助

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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"会话变量以及beachBachbushBach变量。