Windows Phone back button - c#

I need to go back to a particular page when I click on a button. more like custom back button.
I can go exact page, if I know what is the page is,
private void button_click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(Page1));
}
Also I can go back to the previous page by
if(this.Frame.CanGoBack)
{
Frame.GoBack();
}
But how can I jump to the page before the previous page at once?
Any record of page history?

You can use NavigationServices.BackStack to access on an history of your previous pages.
But you have not specific method to go back from 2 pages at once. However, if you would jump to the page before the previous page you have just to called NavigationServices.GoBack() two times.
Moreover, if you would go back to a specific page, you can know its index between backstack and make a loop with the GoBack method.

You can get all pages from BackStack as a List :
var lstAllStackPages = this.Frame.BackStack.ToList();
and make a for loop to get your specific page and navigate to it.

Thank you #CoderDennis, I figured it out. hope it will help to someone else.
I wanted to move back to the Main page if I have come along the Main page,
foreach (PageStackEntry stack in Frame.BackStack)
{
//check if stack contains history of the main page. (Solution name: Helloworld)
if (stack.SourcePageType == typeof(HelloWorld.MainPage))
{
Frame.Navigate(stack.SourcePageType);
break;
}
}

Related

PuppeteerSharp - Access a background page, that opens in a new tab, as a regular page

I'm having issues with a program that generates reddit apps with refresh tokens from a discord command.
I've managed to get to a point, where I can generate the application, get all the relevant information, head over to https://not-an-aardvark.github.io/reddit-oauth-helper/ and from there generate the token, which opens a reddit confirmation page in a new window.
I've tried accessing it in various ways and have gone through multiple different methods, until I landed on using Target.PageAsync() to get the page.
For some reason, Puppeteer only sees the page as an iFrame and only gives this link when getting the Url property - https://www.redditmedia.com/gtm/jail?cb=8CqR7FcToPI - which doesn't lead to anywhere but seems to be related to the very first iFrame from what I've gathered in the HTML.
I've ran out of ideas on how to access the page to press quite literally one button and would appreciate any ideas or solutions on how to solve this or how to generate the refresh token without the use of an external website.
Another two hours later and I managed to figure out a solution.
Since PuppeteerSharp was unwilling to recognize the page, I just subscribed to Browser.TargetCreated at the correct moment with a handler that, after immediately unsubscribing, will log the most recent target (in this case, a javascript calling window.open()) and take the sender as the Browser, will then try to get the pages into an array and with a bit of code to ensure that it doesn't break itself, I finally managed a solution, I feel kinda dumb after three days and 12+ hours of work.
For anyone who might run into a similar situation, here's the snippet of code that made it finally work:
// Bla bla bla code to crawl or do whatever on the main page.
// Immediately subscribe to the target created event with the event handler
// that will handle the background page once it has
// been triggered by a button, link, etc.
browser.TargetCreated += TargetCreatedEventHandler;
}
static async void TargetCreatedEventHandler(object sender, TargetChangedArgs e)
{
// Unsubscribe from the event to
// make sure there are no duplicate unnecessary calls that might break the code.
browser.TargetCreated -= TargetCreatedEventHandler;
// Since I know the sender is the Browser object,
// I cast it it to another Browser used inside the event handler.
Browser eventBrowser = (Browser) sender;
// Get all the pages from the event browser
// and assume the first page is background one (for now)
Page[] pages = await eventBrowser.PagesAsync();
Page page = pages[0];
int counter = 0;
// Iterate through the pages, check if they're the page you were just on,
// use an int to help you keep track of of indexes.
// If it isn't the page you were on, assign the Page object the page
// with the current counter index from pages.
// (basically make sure it doesn't break itself with the wrong order).
foreach (var item in pages)
{
if (item.Url != "Main Page URL HERE")
{
page = pages[counter];
break;
}
counter++;
}
// Do whatever you need to do on your background page here
}

Can you "remember" what link is clicked on a webpage?

I'm working in Visual Studios to create a website for a movie theater (for class) I'm wondering if there's a way to "remember" what link is clicked for a show time? I know I could just create a webpage for each time, but I'd rather track an id, if that's possible!
By clicking the link, they'll go to the right page, but is there any way for me to put into the system what time they clicked? I couldn't find any similar questions here.
you could use sessions to hold the showtime selected. create an LinkButton with an onCommand that creates a session before redirecting them to the seating chart or checkout page.
so say a user is on your movie page and the user selects the 12pm showtime. When the user clicks on the link, it will run what ever is in your code behind. You can create a Session["DateTime"] and set it to the value of the linkbutton. then, redirect to the seating page.
on the seating page, you can check if there's a session. if it finds one, then you can load the seating chart or purchasing page. if it does not find one, you might want to redirect them back to select a time.
HTML example:
<h2>Next Big Movie</h2>
<asp:LinkButton id="lbNBM12pm" runat="server" OnCommand="lb_Click" text="12:00pm"></asp:LinkButton>
Code Behind example:
protected void lb_Click(object sender, EventArgs e)
{
string time = (sender as LinkButton).Text;
Session["TimeSelected"] = time;
Response.Redirect("/nextstep.aspx");
}
and on the next page, you'll read what was passed over in the page load and set the Selected Movie time text:
protected void Page_Load(object sender, EventArgs e)
{
if(Session["TimeSelected"] != null)
{
MovieTime.Text = Session["TimeSelected"].ToString();
}
else
{
//user did not select a time
//possibly redirect them back to the showtimes page.
response.redirect("/previousepage.aspx");
}
}
If I understand it correctly. First, create a table with the following columns:
create table Links
(
id int identity,
link varchar(100),
timeclicked datetime
)
then fire an ajax or a simple HTML POST request to submit what link is clicked in a spefic time.

ASP.NET UpdatePanel: Make browser "back" button return to *last* version of the page

I have page A and page B. You can do the following things in page A:
Do stuff on page A (e.g., choose an item in a list box), which causes an UpdatePanel in page A to be redrawn with additional information.
Move on to page B. This is done with a Button and Response.Redirect.
Now the problem is as follows:
The user does stuff on page A. Page A is now different from its initial state.
The user moves to page B.
The user hits the back button of the browser.
What happens: Page A in its initial state is shown.
What I would like to happen: Page A in its final state is shown (i.e., with the "correct" item selected in the list box).
I know about ScriptManager.AddHistoryPoint! As far as I can see, it does not solve my problem:
I could call AddHistoryPoint every time something is done on page A. This is bad, because it litters the browser history with lots of entries. (A new entry every time a different list box item is selected.) But that's exactly what I want to avoid by using an UpdatePanel! Of course, if there were a ReplaceLastHistoryPoint method, that would be perfect, but I did not find one...
I tried to call AddHistoryPoint right before Response.Redirect, to save only the last state of page A, but, alas, that doesn't work (no history point is saved). This is not surprising, considering how Response.Redirect works.
Is there some solution I have missed? I'm using .NET 3.5SP1, in case it matters.
This is a bit of an old question, but I'll go ahead and provide the mechanism I use for this. The basic idea is that instead of allowing AddHistoryPoint to manage your name-value pairs, just allow it to manage a key to your NameValueCollection that you keep somewhere else, like in your Session cache. Then as subsequent Ajax requests come in, you never make another call to AddHistoryPoint; instead, you just replace your NameValueCollection with the state of the current request.
The only other bit is to keep track of whether you're on your first Ajax call and need to make that first call to AddHistoryPoint or not.
My code looks something like this:
protected void Page_LoadComplete(object sender, EventArgs e) {
if (!ScriptManager.GetCurrent(this).IsNavigating && (IsCallback || IsInAsyncPostback())) {
var state=new NameValueCollection();
//OnCallbackHistory(state); // this gets state for all interested parties
if (state.Count != 0) {
string key=ViewState["HistoryStateKey"] as string; // empty on first AJAX call
if (string.IsNullOrEmpty(key) || ScriptManager.GetCurrent(this).EnableHistory) {
key=CallbackHistoryKeyRoot+Interlocked.Increment(ref callbackHashKey).ToString();
ViewState["HistoryStateKey"]=key;
ScriptManager.GetCurrent(this).AddHistoryPoint("", key);
}
Session[key]=state;
}
}
}
Instead of calling AddHistoryPoint on the server you could call addHistoryPoint on the client using the Sys.Application class, http://msdn.microsoft.com/en-us/library/vstudio/cc488025(v=vs.90).aspx.
So you'd add a client side click listener to the button which would addHistoryPoint on the client before the button does the post back and redirect.

Submit a form from code behind

I'm having trouble implementing a functionality on my c#/asp.net app.
I have a form with a RadioButtonList and a submit button.
The RadioButtonList is generated on Page_Load() from a list of objects I retrieve from the database.
I would like to automatically submit the form if there is only 1 object in the list.
I have access to my Form object, to the submit button etc... but I can't seem to find a solution (in the end I'm looking for a kind of form.Submit() ) method.
Does anyone have an idea of how I could do this ?
Thanks in advance !
EDIT >> Here is the code :
.aspx : http://pastebin.com/0E6T7dqH
.aspx.cs : http://pastebin.com/54payZJP
EDIT2 >>>
As it seems there is no way to do what I wanted to do at first, I ended up using a session variable with a response.redirect()
Source :
http://dotnetslackers.com/Community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspx
Post happens in the client side. As in Page_Load you are currently executing in the server side, just call the code you want to execute on post.
Edit: For actually going to another aspx
public void Page_Load(object sender, EventArgs e) {
if(!IsPostback && OnlyOneItem) {
Server.Transfer("TheOtherPage.aspx");
}
}
Server.Transfer will maintain the entire request, so your post data will be available.
http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.transfer.aspx
Try something like this
In your Page_Load
if(!IsPostBack)
{
if(check for only one object)
{
//your submit code
}
}
I actually had to do something similar, once. Here is a way you can do it.
Asp.Net buttons have a property called PostBackUrl, and it does exactly what you would expect - it controls where the form will post if you click the button.
You can also use the RegisterStartupScript function to render javascript on the page.
Now, with these two pieces, you can achieve your goal.
if(!IsPostBack)
{
if(results == 1)
{
button.PostBackUrl = "next page's url"
//Register script to click the button using RegisterStartupScript
}
}
Now, having shown you this, I will warn you it may not make for the best user experience. When I did it, it was for a very specific case that had no other solution. The page will actually post back to the user, and they will see the page for a moment before the javascript to click the button takes effect. Additionally, when you set a button's PostBackUrl, that means that when it is clicked, your entire form will be posted to the page specified. The code behind for the current page will not fire at all, so if you have any validation, it won't run.
There's nothing wrong with letting the user click the button to submit the form even if they only have one choice. In my experience, users like to feel like they are in control on the system; they don't like it when pages just do things without their input.
Also, there is not really anything wrong with putting the information the next page needs into the session or even a database table, and using Response.Redirect. It's a fairly common practice and works reliably in most scenarios.

Restrict a page navigation to one control

I am making an online exam website where a user is taken to a page where there are multiple choice questions. I want to set it up so that once a user navigates to this exam page, he cannot navigate back. The only way he can get out of it is by clicking the submit button and then he cannot access that page again. The questions on the exam page are in an SQL database. I have everything done, the only thing I need help with is restricting the navigation of the page.
It can be done by using Session varaible.If u have 3 pages say a.aspx,b.aspx,c.aspx then create 3 session variables such as
Session["a"]="some value";Session["b"]="some value";Session["c"]="some value";
On the pageload of the a.aspx check if the session contains value or null.If it is null then u can make sure that it is the first time that page is loaded else do what you want to do if user tries to visit a page which is already viewed.
Please try this code
//Code for page a[a.aspx]
protected void Page_Load(object sender, EventArgs e)
{
if(Session["a"]==null)
{
Session["a"]="Some Value";
}
else
{
// do code if user visit the page again.
}
}
Similarly you needs to do for all the pages.
I hope this will solve your problem.
You could use cookies or session to keep a tab on the pages the user has gone through so you can prevent them from going back.

Categories

Resources