/// <summary> /// Registers current page in navigation history so that user can come back to it later. /// Put it in Page_Load event in "if(!PostBack)" structure in every page that participates in navigation functionality (that uses Navigation class). /// </summary> /// <param name="pageName">Name of current page in form PageName.aspx</param> public static void RegisterPage(string pageName) { NavigationHistoryItem newItem = new NavigationHistoryItem(); newItem.pageName = pageName; newItem.queryString = HttpContext.Current.Request.QueryString; Navigation.history.Push(newItem); Navigation.UpdateHistory(); }
/// <summary> /// Redirects back to previous page that exist in navigation history stack structure. /// (For example if history is A->B->C and current page is C it redirects to B page) /// To return to page it needs to be registered (see Navigation.RegisterPage method). /// </summary> /// <param name="useStoredQueryString">Append (to Url) query string that existed at time the page was registered?</param> /// <param name="queryStringToAppend">Optionaly add key/value pairs in form: A=B ampersand C=D ampersand E=F. Do not include question mark nor spaces.</param> public static void GoBack(bool useStoredQueryString, string queryStringToAppend) { if (Navigation.history.Count > 0) { //Release current page history item Navigation.history.Pop(); Navigation.UpdateHistory(); } if (Navigation.history.Count == 0) { throw new Exception("Navigation history is empty. Can not go back."); } else { //Get previous page history item NavigationHistoryItem previousItem = Navigation.history.Pop(); Navigation.UpdateHistory(); //TODO: If key from queryStringToAppend exist in stored query string also it should replace that key in stored query string //Build query string string queryString = ""; if (useStoredQueryString) { queryString = BuildQueryString(previousItem.queryString); } if (queryStringToAppend != null && queryStringToAppend != string.Empty) { queryString += ((queryString != string.Empty) ? "&" : "") + queryStringToAppend; } queryString = Navigation.AddQuestionMarkIfNotEmpty(queryString); //Go back using history item HttpContext.Current.Response.Redirect( Navigation.GetApplicationRootUrl() + "/" + previousItem.pageName + queryString ); } }