NavigationService.GoBack(); question - c#

When I call NavigationService.GoBack(); it doesn't reload the page.
For example I have Page 1 which is my Login page, then I Navigate to Page 2 to the Settings Page. When I have saved my Settings on Page 2 I wish it to Navigate back to Page 1 and show the new settings that are displayed.
Is there any call I can make where the Navigate Service Goes Back AND forces the page to re-initialise? (ie call the page loaded method).
Thanks

Solved it. Use
protected override void OnNavigatedTo( System.Windows.Navigation.NavigationEventArgs e) { //INSERT RELOAD METHOD HERE }
In the PhoneApplicationPage part of every page

When you navigate to next page the previous page is destroyed (if it does not run the background thread). You have a sevral ways to display settings on page nr 1.
When user log in and go to page 2 save your setting to the Isolated Storage and when he presses the back button use
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
var settings = LoadMySettingsFromIS();
if (settings =! null)
{
// update it here
}
base.OnNavigatedTo(e);
}

Related

How to notify the page when GoBack() happened?

Here is what my app does:
The first page can navigate to the second page, and the second page displays a list of data. The user can choose one of them then the app will bring the data back to the first page.
Sounds easy, but I'm confused with the Windows Mobile Navigation Model.
The first page navigates to the second page, using this code:
this.Frame.Navigate(typeof(SecondPage));
and the second page uses the code below to go back:
this.Frame.GoBack();
How could the first page know if the second page disappeared? I want to update the UI on the first page after the second page disappeared.
Now, I used a static class to keep the data that user picks, but I have no idea when should be the right time to update the first page.
Is there any way to get an event or notification?
This is quite simple, since UWP does this for you. I noticed you're not using MVVM, so you can simply override the OnNavigatedTo event in your page. This event is triggered when navigation to your page is completed (and thus the second screen dissapeared). Simply check for NavigationMode.Back to confirm you're returning and not navigating forward.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.NavigationMode == NavigationMode.Back)
{
// coming from 2nd page, so refresh your data
}
}

How to re-use already bound data in a user control on post back

I have a user control that I dynamically add to a Panel. I also have a couple of other functionality in the aspx page like search which is inside an update panel that causes the page to post back. From what I have read so far is that dynamic control needs to be bound on each page load. This works fine but the issue is that the user control takes a bit of time (like 3s) and thus all request operations takes longer because the user control is being bound every time.
If I load the user control inside the Page.IsPostBack condition and load it only on page load then user control is visible on post back but all the associated events in the user control is not fires.
So, Is there a way in which I can store the user control data in a session and then bind it if I know that the data is not going to change and hence reducing the 3s delay to load the user control.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadUserControl(); // Only loaded on page load.Faster, but user control
//functionalities break on post back. Maybe
// because it is not loaded again.
}
//LoadUserControl(); // User control loads fine. But even a totally unrelated
// post back causes the already loaded User Control to load
// again. 3s delay :(
}
The method used to load the user control is
private void LoadUserControl()
{
var control = LoadControl("A Dynamic ascx page");
control.ID = "contentControl";
panel1.Controls.Clear(); //panel1 is the placeholder in aspx page
panel1.Controls.Add(control);
Session["LoadedControls"] = control;
}
So I tried to save the loaded control in session and add in to the placeholder panel as such
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadUserControl(); // Only loaded on page load.Faster, but user control
//functionalities break on post back. Maybe
// because it is not loaded again.
}
if ((Control)Session["LoadedControls"] != null)
{
panel1.Controls.Clear();
panel1.Controls.Add((Control)Session["LoadedControls"]);
}
}
this does not work as expected either. I do not get any of the data present in the user control.
The user control has 2 RadGrid and 2 RadHTMLChart. I did not want to add it since this is already a huge post. the data for the controls in user controls is also being stored in session for binding again. But I am unable to find a way to add the user control dynamically with the bound data.

Immediately redirect to another page in WPF

I have a page that several other pages navigate to. However in some circumstances the user should not see this page, so I want to send them to another page instead.
Rather than update the rest of the calling code, I just want to change this page to handle it.
public MyPage()
{
Loaded += MyPage_Loaded;
InitializeComponent();
// Other stuff
}
void MyPage_Loaded(object sender, RoutedEventArgs e)
{
if(condition)
NavigationService.Navigate(someUri);
}
Since the NavigationService isn't available in the constructor I have to hook up to the Loaded event and do the redirect there. The problem is that the page has already been loaded and displayed to the user. There is also a slight delay before redirecting the user.
Is there a better way to do this where the redirect is seamless?

Navigation caching only when navigating back

I have a little bit of experience programming Android apps.
And now i am wondering if the same thing is possible:
(Android:)
When navigating in an android app between activities, the state of the previous activity is hold. By pressing the back button, you get the previous activity the way you left it. When navigating towards an activity, this opens the activity in the default state.
(UWP:)
Navigating between pages:
I have a page which should be cached for when i navigate back.
But when I navigate towards the page, it should open the page in the initial state.
How is this possible?
(I already am able to enable navigationcachemode. I wonder if i can disable it, or create a new instance of the page for example.)
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
NavigationCacheMode = NavigationCacheMode.Disabled;
base.OnNavigatedFrom(e);
}
Just disable the NavigationMode when you're navigating back from a page. So you force the creation of a new page when you'll navigate again to that page (and the NavigationMode will be set to the NavigationMode you set in the constructor or XAML).

Response.Redirect() disables back-button

As far as I understand, Response.Redirect("http://stackoverflow.com"); tells the browser to initiate a request to another URL.
One of the results of this is that the browser "remembers" the redirection, and allows pressing "back."
However, I have a website where Response.Redirect disables the ability to press the browser's "Back" button, as if the browser had opened a new window. (Browsing history is not forgotten, unlike with Server.Transfer.)
The redirection used to work properly in the past, so I suspect the problem has something to do with the IIS server (IIS7).
I apologize in advance if this question should be moved to ServerFault.com.
UPDATES:
Here is some code:
protected void btnClickMe_Click(object sender, EventArgs e)
{
// ...
// some server-side logic
// ...
Response.Redirect("NewPage.aspx?ProductID=" + idNum);
}
Regarding "disables the ability to press the browser's 'Back' button", what I meant is that the button cannot be pressed. Same as when you open a new window. The button is gray, and clicking it has absolutely no effect.
UPDATE 2:
This has been tested with IE6 and IE8.
The problem was NOT with the Response.Redirect();.
When I was on OldPage.aspx, I entered a new URL in the address bar. Once the browser loaded the new site, it disabled the back-button.
Conclusion: There is something wrong with OldPage.aspx, not the redirection to NewPage.aspx.
I still don't know why THIS happens, but this is an entirely different question.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) //check if the webpage is loaded for the first time.
{
ViewState["PreviousPage"] =
Request.UrlReferrer;//Saves the Previous page url in ViewState
}
}
protected void btnBack_Click(object sender, EventArgs e)
{
if (ViewState["PreviousPage"] != null) //Check if the ViewState
//contains Previous page URL
{
Response.Redirect(ViewState["PreviousPage"].ToString());//Redirect to
//Previous page by retrieving the PreviousPage Url from ViewState.
}
}

Categories

Resources