Handling back navigation in WebBrowser control preserving the previous scroll position - c#

I know there a lot of questions asking about how to handle the back navigation in the WebBrowser control in WP7. And a lot of answers too, to achieve the same, using stacks, InvokeScript, etc.., and I am currently using this approach, and it works fine (JavaScript):
private void BackButton_Click(object sender, EventArgs e)
{
try
{
webBrowser.InvokeScript("eval", "history.go(-1)");
}
catch (Exception)
{
}
}
But however say when a user scrolls down to section 'n' in a webpage, and a link from there takes him to a new page, and then the back button is pressed, the page successfully navigates back to the previous page, but is at the top of the page, i.e., section '1'.
I would however like to preserve the user's scroll position in the previous page, and send him back to the previous page and down at the position from where the link took him, so that he doesn't have to scroll down all over again, i.e., if a link at section 'n' takes him to page2 from page1, after clicking back, it should take him back to page1 and scrolled down till section 'n'.
Thanks.
(The back button that I'm using is a button on the ApplicationBar and not that of the phone's.)
Note: This is how the back button works in the Internet Explorer browser of the Windows Phone, preserving scroll positions.

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

UWP Xbox One page navigation when using WebView

Usually when running a UWP app on Xbox the B button on the controller is handled automatically and will return you to the previous page.
I have a page which contains a WebView, when you use the directional buttons to place the focus box around that control, the B button no longer responds. You can use the A button to take control of the WebView and display the pointer and the B button then will return focus back as above but I cannot navigate back using the B button until you move the focus box to a different control. This also happens using AdControl since this uses WebView.
I have tried to capture KeyDown:
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
Debug.WriteLine(args.VirtualKey.ToString());
}
This responds with GamePadB, GamePadX etc but not when the focus box is around the WebView.
Is there anyway I can find out when the GamePad buttons (specifically B) are pressed when the focus box is around the WebView (or AdControl) and the control isn't engaged so I can manually invoke the backstack navigation?
Since this issue happens when using the XY focus mode for the app, if your OS version is 14393 or higher, one workaround for this issue is to use the mouse mode for this page which contains the webview by setting RequiresPointer="WhenFocused" as following:
<Page RequiresPointer="WhenFocused">
...
</Page>
And set another page to XY focus mode by using the following code in the app.xaml.cs:
this.RequiresPointerMode =
Windows.UI.Xaml.ApplicationRequiresPointerMode.WhenRequested;
For more information, please try to refer to the following article:
https://msdn.microsoft.com/en-us/windows/uwp/input-and-devices/designing-for-tv#mouse-mode

Clicking multiple times on the same redirect button in ASP.NET causes weird results

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.

WebBrowser class wont submit data to webform untill main thread is dead

I'm having trouble my application which is supposed to write text in some textboxes on a website and click OK button. But then the problem comes, the site loads a new page and you have to confirm the previous commands by clicking a new OK button. What I need help with is for my C# code to execute the commands as they are called and no do them once the running thread is dead.
Example of my code:
private void startScript_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("login_box").InnerText = "NAME";
webBrowser1.Document.GetElementById("password_box").InnerText = "PASS";
webBrowser1.Document.GetElementById("login_button").InvokeMember("click");
//This is where i want the first texts and click to be submitted so it then
//can click on the confirm button the the next page
webBrowser1.Document.GetElementById("confirm_login").InvokeMember("click");
//Submit the confirm click
} //This is where all the information given in the code is actually given/executed to the browser
So to clear up the question. What I need to do is; give the information to the browser directly when the webBrowser1.Document...; is called. A command to submit would also work (webBrowser1.SubmitData) or something like that.
The WebBrowser navigation, which is taking place when you click the button, is an asynchronous operation, it doesn't complete instantly.
I can't guess what your code is doing immediately after InvokeMember("click"), but if you're trying to access WebBrowser.Document right away, or doing something like Thread.Sleep, that won't work. It worked when followed with MessageBox.Show, because MessageBox.Show runs its own modal message loop.
You need to handle WebBrowser.DocumentCompleted first and then access the document. There are a few ways of doing that, here is one of them.

Postback on a submit button

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.

Categories

Resources