【问题标题】:Passing session variable from one page to another将会话变量从一页传递到另一页
【发布时间】:2014-11-15 00:31:02
【问题描述】:

我之前用 PHP 做过这个,现在我想用 C# 做这个。我想我可能走在正确的轨道上,但在此过程中出了点问题。

我有一个“创建订单”页面,其中包含用户想要购买的一篮子商品。在此页面上,他们可以按名称或 sku 搜索产品,如果他们输入了有效的内容,则会显示结果的网格视图。然后他们可以导航到添加/更新页面,在那里他们可以将新产品添加到购物篮或编辑/更新已经存在的产品。这会将他们带回购物篮页面。

在“创建订单”页面上,我正在根据是否在 url 查询字符串中找到购物车 ID 来设置一个带有购物车 ID 的会话变量:

    protected void CreateSessionVariable()
    {
        string session = "";
        if (Request.QueryString["CartID"] != "" & Request.QueryString["CartID"] != null)
        {
            session = Request.QueryString["CartID"];
            Session["CartValue"] = session;
        }
        else
            Session["CartValue"] = "";
    }

然后我称之为 Page_Load 函数:

        if (!Page.IsPostBack)
        {
            BindBasketGrid();
            //call function
            if (Session["CartValue"] != "" & Session["CartValue"] != null)
            {
                string CartCode = Request.QueryString["CartID"];
                CartIDLbl.Visible = true;
                CartIDLbl.Text += CartCode;
            }
            else
                CreateSessionVariable();
        }

在添加/更新页面的 Page_Load 中,这就是我所调用的(它在 !Page.IsPostBack 中调用:

                if (Session["CartValue"] != "" & Session["CartValue"] != null)
                {
                    string CartCode = Session["CartValue"].ToString();
                    CartIDLbl.Visible = true;
                    CartIDLbl.Text += CartCode;
                }

事情运行了一会儿——会话变量成功地从“创建订单”页面传递到“添加/更新”页面。但是,当返回创建订单页面时,不再设置会话变量。

我是不是设置不正确?

【问题讨论】:

  • 在 Patrick 的回答之上,您的第一个代码块中的 Else 语句缺少右括号,第二个代码块中的 Else 块缺少右括号。跨度>

标签: c# asp.net session


【解决方案1】:

您使用的& 使条件Session["CartValue"] != "" & Session["CartValue"] != null 成为按位与。

在 C# 中,您需要 && 进行 AND:

if (Session["CartValue"] != "" && Session["CartValue"] != null

这个语句可以写成这样更简单:

string s = Session["CartValue"] as string;
if (!string.IsNullOrEmpty(s))

【讨论】:

  • 好的,我已经做了这个更改,但是会话变量仍然没有在两个页面之间成功传递......
【解决方案2】:

您确定“创建订单”页面中的Session变量CartValue是否写得很好,生成会话变量的条件是否正在发生?

我建议你在 !IsPostback 条件之外检查会话变量的值:

    //Create a private variable.
    private string CartValue;

    protected void Page_Load(object sender, EventArgs e)
    {
        //Check your session variable "will be empty if the session variable is null".
        CartValue = Session["CartValue"] != null && !string.IsNullOrEmpty(Session["CartValue"].ToString()) ? Session["CartValue"].ToString() : "";

        if (!IsPostBack)
        {
            //Your code...
        }
    }

【讨论】:

    【解决方案3】:

    检查会话变量如-

    Session["CartValue"] != "" & Session["CartValue"] != null
    

    错了。

    可能的意外参考比较;要进行值比较,请将左侧转换为“字符串”。您应该检查 Session 变量是否为 NULL 或 NOT。如果不为 NULL,则 Session 变量的值为 null 或空。

    if (Session["CartValue"] != null && !string.IsNullOrEmpty(Session["CartValue"].ToString())){
        // your code..
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      相关资源
      最近更新 更多