I want to get the text from blankpage.xaml to Mainpage.xaml
I try to use this on MainPage.xaml:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
enter.Text = (App.Current as App).trytext;
}
but there is an error that trytext could not found so i use and if statement:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (enter.Text == "")
{
enter.Text = "";
}
else
{
enter.Text = (App.Current as App).trytext;
}
}
But when I get the text from blankpage there is no text when I get back to mainpage
please help!
I'm not sure what you're trying to do, but it seems like you're trying to creating a variable that's accessible through the whole application?
If you want to pass some data from page 1 to page 2, you can pass the information in the Navigate method.
Probably you're calling something like
Frame.Navigate(typeof(MainPage));
The Navigate method has an extra parameter in which you can pass your info. Call it like Frame.Navigate(typeof(MainPage), enter.Text);
In the OnNavigatedTo you can get the value from the NavigationEventArgs.
probably something like:
enter.Text = e.Parameter.ToString();
Related
I am creating a WindowsPhone app and (at the moment) I have three pages. Page1, page2 and page3. Page1 has two (2) textboxes that accepts the fist and last name, txtFN and txtLN and a button. The button takes you to the form2, that asks you the nickname. That has one textbox and a button. When the user clicks the button, it takes him the form3 that has a textblock. The nickname the user inputs in form2, displays in form3, on the textblock. I have gotten that part correct. Now, I am trying to do this: if the user does not input anything in the nick name section, I would like to show the first name in form three. Here's my code for form2->form3 event:
in form2:
private void Button_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(Page4_phone_Number_extraction), "Alright " + Texbox_nickname_extraction.Text + "!");
}
in form3,
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Textblock_nickname_display.Text = e.Parameter.ToString();
}
And this works like a charm..
Again, for the life of me, i can't think of how to have the first name show up in form3, if there is no user input.
One option would be to store the data in a local app settings like:
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["FirstName"] = txtFN.txt;
in page 1.
You can then extract it in page 3 as:
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Textblock_nickname_display.Text=localSettings.Values["FirstName"].ToString();
You can also use OnNavigatedFrom() to achieve the same functionality, but it involves more work:
In Page1:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
Page2 destinationPage = e.Content as Page2;
if (destinationPage != null)
{
destinationPage.FirstName = txtFN.Text;
}
}
Make sure to have a string FirstName {get;set;} in Page2. Now use the same method to send the variable to Page3 too. You can then use the FirstName variable in Page3.
Needless to say, the first method is simpler.
Microsoft's documentation contains an example that shows that you shouldn't use OnNavigatedTo(NavigationEventArgs e) like you did but instead:
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
string name = e.NavigationParameter as string;
if (!string.IsNullOrWhiteSpace(name))
{
tb1.Text = "Hello, " + name;
}
else
{
tb1.Text = "Name is required. Go back and enter a name.";
}
}
I don't understand why or what I'm doing wrong, but I get a null reference exception when the follow code is executed in my Windows Phone 8.1 application:
First, the application navigates and passes the selectedStation to the next page...
Code from the MainPage:
// When an item is selected, go to the next page and pass info
private void listBoxStations_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Get the selected station item
CompleteStation selectedStation = (CompleteStation)this.listBoxStations.SelectedItem;
this.Frame.Navigate(typeof(StationInformationPage), selectedStation);
// Make sure we set the selected index to -1 after item is selected so
// when we come back to page, no items are selected
this.listBoxStations.SelectedIndex = -1;
}
Here is the code that is getting the null error in the next page:
private CompleteStation station;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.navigationHelper.OnNavigatedTo(e);
this.station = (CompleteStation)e.Parameter;
AddInformation();
}
private void AddInformation()
{
this.txtStationTitle.Text = station.StationName;
// Add more information here
}
The error is specifically happening when I try to change the txtStationTile.Text to station.StationName.
If I take out the code that changes the textbox, and I step through the program, it shows that the station variable is not actually null by the end of the OnNavigatedTo method...
Any help would be greatly appreciated!
-Johan
It seems that it's not the station that's null, but the this.txtStationTitle.
You're doing everything in OnNavigatedTo while the page (XAML) including the TextBlock you're trying to change is not completely loaded, so the TextBlock is null and when you try to do this.txtStationTitle.Text, you get a NullReferenceException.
However, if you called AddInformation in Loaded event handler of the Page, then you'd be sure that the page is fully loaded and TextBlock would not be null anymore.
public SomePage()
{
this.InitializeComponent();
this.Loaded += SomePage_Loaded;
}
void SomePage_Loaded(object sender, RoutedEventArgs e)
{
AddInformation();
}
This type of exception is usually very easy to debug. Putting a breakpoint on the following line:
this.txtStationTitle.Text = station.StationName;
and examining this.txtStationTitle and station would make it really simple to find out what exactly is null.
I am using App.RootFrame.GoBack(); but it does not refresh the page (which makes sense).
I would like to enforce a page refresh... Anyway of doing this?
Possible duplicate of this.
In any event, you probably want to use the NavigationService as follows:
if(NavigationService.CanGoBack())
{
NavigationService.GoBack();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// ensure view has the data it needs
}
This seemed to do the job well.
IEnumerable<JournalEntry> pages = App.RootFrame.BackStack;
string pageURL = pages.First().Source.OriginalString;
App.RootFrame.Navigate(new Uri(pageURL + "?Refresh=true&random=" + Guid.NewGuid(), UriKind.RelativeOrAbsolute));
on a 'GoBack()' the OnNavigatedTo() method will be run, but the constructor for the page won't be. Therefore you need to refresh the page yourself in the OnNavigatedTo() method.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Refresh the data in here
}
Also I would suspect that you would be better of using the navigationservice to go back i.e:
NavigationService.GoBack();
I'm wondering what is my issue on passing a variable from page to page using asp.net session.
I've stripped the code down to just one text box to see whats going on. I'm just trying to take the value of a text box and display it on a confirmation page. When the button is clicked it transfers me to the second page but there label is blank. Yes my post back url is pointing to the second page.
Here is the button click:
protected void submit_Click(object sender, EventArgs e)
{
string name = txtFirstName.Text.Trim();
Session["name"] = name;
}
Here is the page load of the second page:
protected void Page_Load(object sender, EventArgs e)
{
lblName.Text = (string)(Session["name"]);
}
Unless I've been looking at this to long and missed something. I've already read "How to: Read Values from Session State" from MSDN.
You say that you've set the PostBackUrl to your second page. If you're going to do it that way, you need to use Page.PreviousPage to get access to your textbox. But this is the easiest way:
Firstly, leave the PostBackUrl alone. Setting the PostBackUrl to your second page means that you're telling the SECOND PAGE to handle your button click, not the first page. Hence, your session variable never gets set, and is null when you try to pull it.
This should work for ya.
And yes, you can also do this with a QueryString, but if its something that you don't want the user to see/edit, then a Session variable is better.
protected void submit_Click(object sender, EventArgs e)
{
string name = txtFirstName.Text.Trim();
Session["name"] = name;
Response.Redirect("PageTwo.aspx");
}
Then in the second page (You don't REALLY need the ToString()):
protected void Page_Load(object sender, EventArgs e)
{
if (Session["name"] != null)
{
lblName.Text = Session["name"].ToString();
}
}
EDIT -- Make sure that your button click actually gets fired. Someone can correct me wrong on this, as I do most of my work in VB.NET, not C#. But if you don't specify the OnClick value, your function won't get called.
<asp:Button ID="Button1" runat="server" Text="Click Me!" OnClick="submit_Click" />
The code you have posted looks fine, so your problem is probably with setup.
Check this link ASP.NET Session State Overview and pay particular attention to the sections on Cookieless SessionIDs and Configuring Session State.
I don't think you added the session. This is how I have done mine.
First Page
protected void btnView_Click(object sender, EventArgs e)
{
foreach (ListItem li in lbxCheckDates.Items)
{
if (li.Selected == true)
{
lblMessage.Text = "";
string checkDate = lbxCheckDates.SelectedItem.Text;
Session.Add("CheckDates", checkDate);
Page.ClientScript.RegisterStartupScript(
this.GetType(), "OpenWindow", "window.open('Paystub.aspx','_newtab');", true);
}
}
}
Second Page
protected void Page_Load(object sender, EventArgs e)
{
string checkDate = (string)(Session["CheckDates"]);
//I use checkDate in sql to populate a report viewer
}
So with yours, I think you need..
protected void submit_Click(object sender, EventArgs e)
{
string name = txtFirstName.Text.Trim();
Session.Add("Name", name);
}
I think what you have in the second page should work, but if it doesn't, add ToString() to it like..
lblName.Text = (string)(Session["name"]).ToString();
Let me know if this helps!
Are you doing a redirect after setting the session variable on the first page, if so you it will not work correctly (unless you know the trick). Checkout this article on making it work. Basically, the way to make this work is to the overload redirect method.
Response.Redirect("~/newpage.aspx", false);
The false parameter prevents .net from terminate processing on the existing page (that actually writes out the session state)
For Second Page
protected void Page_Load(object sender, EventArgs e)
{
if (Session["value"] != null)
{
Label1.Text = Session["value"].ToString();
}
else
{
Label1.Text = "Sorry,Please enter the value ";
}
}
You can use Server.Transfer() instead of Response.Redirect()
For first page, use this:
protected void Button1_Click(object sender, EventArgs e)
{
string value = TextBox1.Text;
Session["value"] = value;
Response.Redirect("~/Sessionpage.aspx");
}
I just started developing for windows 8, and I'm having some difficult in passing parameters in navigation between pages.
What I am trying to do is: I have two pages (page1 and page2) In page1, there's a menu with a button. When I click in this button, the click event should pass as a parameter an possible URI or path, that will set the source of a image in page2.
this is the code in page1:
private void Button_Click_2(object sender, RoutedEventArgs e)
{
//this.Frame.Navigate(typeof(SplitPage), "ms-appx:/Imgs/1.png");
this.Frame.Navigate(typeof(SplitPage), new Uri("ms-appx:/Imgs/1.png", UriKind.RelativeOrAbsolute));
}
and page2, for receiving the Uri:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var imgSource = e.Parameter as ImageSource;
this.imgParaPintar.Source = imgSource;
}
I'm noticing that imgSource is receiving nothing, it keeps as null.
So, any clues on what I'm doing wrong or what I am missing?
I think you need code like this (untested):
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.imgParaPintar.Source = new BitmapImage((Uri)e.Parameter);
}
You might need to add using Windows.UI.Xaml.Media.Imaging; to your file, if you don't have it there already.
It's worth noting, as stated in the comments here:
http://answers.flyppdevportal.com/categories/metro/csharpvb.aspx?ID=4b847d71-9cd5-4457-add9-f68e457b23ff
That you can only pass primitive types as navigation parameters.