0

I'm trying to make a script that checks, if a user has the right age before joining a team. If the user age doesn't match the team age, the script should stop at this page, and require the user to click the button "BackToLastPageBtn" go back to the previous page, which uses a variable called "BackToLastPage", which gets its value from 'Session["currentUrl"]', before it is reset at Page_load. The problem is, that it tells me the value is null, when clicking the button. I don't know why it is null, when i add the value to "BackToLastPage", BEFORE resetting Session["currentUrl"]. I hope someone can tell me, and guide me in the right direction.

The CodeBehind - script

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

8
  • I should add, that i tried to comment out the entire script, and added "Response.Write((string)Session["currentUrl"]) in Button_Click-method, which wrote out the right url on the page, so i guees it has something to do with the new variable "BackToLastPage" on pageLoad, but i am not sure how to fix it, as it seems logic to me, the way i did it. Commented Jan 14, 2014 at 19:06
  • Is it possible that the button click is by passing the page load event? Try change your private variable to: private string BackToLastPage {get { return if(Session["CurrentUrl"] == null) return "" else return Session["CurrentUrl"].ToString(); } } Commented Jan 14, 2014 at 19:18
  • Now i just get a page saying: Object Moved to here. Commented Jan 14, 2014 at 19:33
  • If i change buttonclick to Response.Write(BackToLastPage), nothing happens. Commented Jan 14, 2014 at 19:35
  • Then session["CurrentUrl"] is null. Is the previous page setting thate value in the session? Commented Jan 14, 2014 at 19:36

1 Answer 1

1

Since you are setting session["CurrentURL"] to null in page_load. When the event is fired it no longer exists. Below is code that i got it working. I had to cut some of your code out since i dont have the definition of all your classes. Private properties do not persist through postbacks. If you wanted it to work the way you have it, you should save the previoius url in a hidden field on the page itself.

Page One:

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

Page Two:

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

You can also try this, Store the return url in a hiddenfield and only set it if it is not a page postback:

Markup 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>

Code Behind:

    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);
    }
Sign up to request clarification or add additional context in comments.

4 Comments

I did try your first answer (before the edit). It worked as long as i didn't put in this line in buttonClick: Session["CurrentUrl"] = null;
Updated the first answer by using a temporary string to store the value before updating the session value to null.
But then tried removing the same line in the original code (along with your first suggestion). Same result: The button worked. So i guess it is the session reset that makes the troubles. I have not tried your second suggestion yet. Btw. I think i need to do a little reading about hidden fields, as it is a topic i know nothing about. :)
Your first answer worked great (after you edited it) :) I think I need to know more about hiddenfields, before using it, but nice to have options. It might be the way to go for future projects. Thank you for your time and great help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.