无法将会话传递到下一页.

问题描述:

当我单击LinkBut​​ton时,我试图将我的categoryName切换到下一页.但是,当我调试我的代码时,它没有进入if语句并跳过它.我不知道到底是什么问题.我在哪里出错了?

这是我第一页的.cs代码:

I am trying to Session over my categoryName over to the next page when clicked on the LinkButton. However, when i debug my codes, it didnt go inside the if statement and skipped it. I do not know what exactly the problem is. Have i gone wrong anywhere?

Here is my .cs code for the first page:

categoryName.Text = Session["categoryName "].ToString();
        Response.Redirect("Category2.aspx");



这是我第二页的.cs代码(我将此代码放在Page_Load中):



Here is my .cs code for the second page (I place this code in Page_Load):

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["categoryName "] != null)
        {
            categoryName .Text = Session["categoryName "].ToString();
        }
    }

您永远不会设置会话值.设置会话值:
You never set the session value. To set a session value:
Session["name"] = value;


声明变量.

字符串CategoryName =//某物.从数据库获取;


在单击链接按钮或链接按钮之前,请单击
设置
Declare a variable.

string CategoryName=//something. get from database;


Before clicking on linkbutton or in link button click
set
Session["categoryName"]=CategoryName;