从会话变量定义和向变量添加值

时间:2014-01-14 19:00:50

标签: c# asp.net variables session session-variables

我正在尝试创建一个脚本来检查用户是否在加入团队之前具有合适的年龄。如果用户年龄与团队年龄不匹配,则脚本应停在此页面,并要求用户单击“BackToLastPageBtn”按钮返回上一页,该页面使用名为“BackToLastPage”的变量,该变量获取其来自'Session [“currentUrl”]'的值,在Page_load重置之前。 问题是,它在单击按钮时告诉我值为null。 在重置Session [“currentUrl”]之前,当我将值添加到“BackToLastPage”时,我不知道为什么它为null。我希望有人可以告诉我,并指导我朝着正确的方向前进。

CodeBehind - 脚本

public partial class JoinTeam : System.Web.UI.Page
{
    //Defining Go back variable
    private string BackToLastPage;

    protected void Page_Load(object sender, EventArgs e)
    {
        int BrugerId = Convert.ToInt32(Session["BrugerId"]);
        int TeamId = Convert.ToInt32(Request.QueryString["HoldId"]);

        //Adding value to go back variable from sessionurl
        BackToLastPage = (string)Session["CurrentUrl"];

        //Resets sessionurl.
        Session["CurrentUrl"] = null;

        if (Session["brugerId"] != null)
        {

            if (ClassSheet.CheckIfUserAgeMatchTeamAge(BrugerId, TeamId))
            {
                ClassSheet.JoinATeam(BrugerId, TeamId);

                if (BackToLastPage != null)
                {
                    //Uses the new savedUrl variable to go back to last page.
                    Response.Redirect(BackToLastPage);
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }

            else
            {
                AgeNotOk.Text = "Du har ikke den rigtige alder til dette hold";
            }
        }
        else
        {
            //Not saving last page. Need to find solution.
            Response.Redirect("Login.aspx");
        }
    }

    //NOT WORKING...
    protected void BackToLastPageBtn_Click(object sender, EventArgs e)
    {
        //Go back button

        //Response.Write(BackToLastPage);
        Response.Redirect(BackToLastPage);
    }
}

Error Message

1 个答案:

答案 0 :(得分:1)

由于您在page_load中将session [“CurrentURL”]设置为null。当事件被触发时,它不再存在。下面是我得到它的代码。我不得不削减你的一些代码,因为我没有你所有类的定义。私有财产不会通过回发持续存在。如果您希望它以您拥有它的方式工作,您应该将previoius url保存在页面本身的隐藏字段中。

第一页:

    protected void Page_Load(object sender, EventArgs e) 
    { 
        Session["CurrentUrl"] = Request.Url.ToString();
        Response.Redirect("~/SecondPage.aspx");
    }

第二页:

    private string BackToLastPage { get { return (Session["CurrentUrl"] == null) ? "" : Session["CurrentUrl"].ToString(); } }

    protected void Page_Load(object sender, EventArgs e)
    {
        int BrugerId = Convert.ToInt32(Session["BrugerId"]);
        int TeamId = Convert.ToInt32(Request.QueryString["HoldId"]);

        if (Session["brugerId"] != null)
        {
            //CUT CODE OUT DONT HAVE YOUR DEFINITIONS
            Response.Write("brugerid was not null");
        }
    }

    protected void BackToLastPageBtn_Click(object sender, EventArgs e)
    {
        //YOU SHOULD SET THE CURRENT URL TO NULL HERE.
        string tempUrl = BackToLastPage;
        Session["CurrentUrl"] = null;
        Response.Redirect(tempUrl);
    }

您也可以尝试这样做,将返回网址存储在隐藏字段中,只有在不是网页回发时才设置它:

标记HTML:

<form id="form1" runat="server">
<div>
    <asp:Button ID="btnOne" runat="server" OnClick="BackToLastPageBtn_Click" Text="Button One" />
    <asp:HiddenField ID="hfPreviousUrl" runat="server" />
</div>
</form>

代码背后:

    private string BackToLastPage //THIS WILL NOW PERSIST POSTBACKS
    { 
        get { return hfPreviousUrl.Value; } 
        set { hfPreviousUrl.Value = value;}
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)//THIS PREVENTS THE VALUE FROM BEING RESET ON BUTTON CLICK
            BackToLastPage = (string)Session["CurrentUrl"];

        int BrugerId = Convert.ToInt32(Session["BrugerId"]);
        int TeamId = Convert.ToInt32(Request.QueryString["HoldId"]);

        //Resets sessionurl.
        Session["CurrentUrl"] = null;

        if (Session["brugerId"] != null)
        {
            Response.Write("brugerID was not null");
        }
        else
        {
            //REMOVED FOR TEST PURPOSES
            //Response.Redirect("Login.aspx");
        }
    }

    protected void BackToLastPageBtn_Click(object sender, EventArgs e)
    {
        Response.Redirect(BackToLastPage);
    }