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
}
}
Related
Here's some background to help better understand the problem:
The company I work for makes x-ray scanners for security purposes. We have a page in our web app for viewing current scan activity. Currently, it's set to refresh every minute. I've been tasked with reworking the page so that it's event driven, refreshing every time a scan is saved. The information is displayed in a GridView, which is inside an update panel. The first column contains a thumbnail image of the scan, and an Ajax HoverMenuExtender for the purpose of enlarging the thumbnail when the cursor hovers over it.
When the page loads, everything works fine. I assume this is because all the extenders are created before the OnPreRender event is handled. Upon my scan event being fired, however, it fails when the GridView.DataBind() method is called. I assume this is because the OnPreRender event already occurred when the page first loaded and, as the exception says, extender controls may not be registered after prerender.
I've done a lot of digging on this exception, but have yet to find a solution that works. I read that moving the ToolkitScriptManager to the master page can fix it, but it's already on the master page. I also tried overloading the OnInit and OnPreRender handlers as follows:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (this.DesignerMode)
{
this.EnsureChildControls();
}
this.Page.RegisterRequiresControlState(this);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}
I tried putting that on the current activity page, the master page, and the base page, all to no avail.
I've come across slews of answers simply saying to register the controls before the prerender, which of course makes sense, but since new controls are being created dynamically upon event trigger after the initial page load, how can I accomplish that? Unfortunately, I just don't understand this well enough to know what else to try.
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?
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).
When I click multiple times on a button that performs a server-side redirect using ASP.NET, thing can get weird. Sometimes I get ViewState errors, other times the page is only partially loaded.
The code for the OnClick event of the button is simple:
HttpContext.Current.Response.Redirect(targetUrl);
If I have something like:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Something();
}
}
in the page I'm redirecting to, the Something() function won't be called if the button is pressed more than once in rapid succession.
Is this normal? What could be the cause of these weird issues when pressing buttons multiple times quickly?
Ok as far as i know i could explain the weird result as below:
the first time you click the button, server will do the redirect and since its the first time you hit the page from another page it will not be a postback, but the second time you click the button in the server you will be already redirected and it will see the request as postback because on the server your already on that page, since its redirected you in the first time, at the end you will get the response of the last click which will be a postback in the server.
to avoid this issue, you should make a loading panel appear on the button, or disable the button before you go to the server using javascript.
I have a page that has 4 tables. Initially when the page is loaded, it shows 1 & 2. Thats working fine. On Post back(When Submit is clicked), it should show 3 & 4. Even thats working fine(code shown here). When the submit is clicked again, it has to call updatePaymentInfo() and redirect. Is there something to write as a condition to call UpdatepaymentInfo() because when submit is clicked, it is taking as an other postback and showing me 3 & 4 again.
protected void imgbtnSubmit_Click(object sender, ImageClickEventArgs e)
{
try
{
if (Page.IsPostBack)
{
trtest.Visible = false;
trCCandBilling.Visible = true;
trtest2.Visible = true;
}
else
{
UpdatePaymentInfo();
Response.Redirect(ApplicationData.URL_MERCHANT_ACCOUNT_HOME, true);
}
}
}
My thought on the easiest way to do this is to have two image submit buttons in the same place. Button A is the one you already have button B is a new one that whose submit handler runs UpdatepaymentInfo and redirects.
Button B starts off invisible while button A is visible. When Button A is clicked in addition to the visibility changes you hide button a and show button B. Then when they click button B the right stuff happens.
Its not that elegant though.
Another solution might be storing values in the page to indicate the current page state that you can then check on button click.
It sounds like you're having trouble managing the current state of your page. You could try:
Having a second submit button. It would be stylistically indistinguishable from the first, and would be hidden/shown accordingly, but would have its own click event.
Placing a hidden form value on the page to track the current "step" of the process.
Breaking the page into two pages, since from the user's perspective it's clearly a two-page process.
My personal favorite, move to MVC :) Though it's understandable if you're stuck in a pre-existing WebForms app and there's just no budget to re-write it.
I guess that imgbtnSubmit_Click handles Click event of the Submit button so this method will be called only during the postback so the condition is incorrect.
I would not use this approach. ASP.NET contains controls which support these requirements. Check MultiView and Wizard. Create separate view with table 1 & 2 and button and another view with table 3 & 4 and button. Button on the first view will switch the view and button on the second view will call the method and redirect.
Another possible way to do this is keep your current set up and add a command argument to the button. By default it has some argument that you check on the first click. Then checking the command argument on the first click you do your showing and change the command argument to be something different. So on the next button click you do the work associated with the second command argument. Thus flipping the work done without having to hide or show a new control.