reading from text box and passing to another page - c#

I want to read text from TextBox and then send it to another page. But on another page I keep geting empty string.
why this doesn't work ?
I have this on page 1:
public string _beseda()
{
return textBox1.Text;
}
and on page 2 where I should retrieve this string:
private void button1_Click(object sender, RoutedEventArgs e)
{
Page2 neki = new Page2();
MessageBox.Show(neki._beseda());
}

There are two strategies to pass data between pages in windows phone.
Use App.cs
Pass data as parameter values during navigation
1. Using App.cs
Open up App.cs code behind of App.xaml write:
// To store Textbox the value
public string storeValue;
In Page1
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
App app = Application.Current as App;
app.storeValue = textBox1.Text;
}
on Page2
private void button1_Click(object sender, RoutedEventArgs e) {
App app = Application.Current as App;
MessageBox.Show(app.storeValue);
}
2. Passing values as parameters while navigating
Before navigating embed textbox value to the Page Url
string newUrl = "/Page2.xaml?text="+textBox1.Text;
NavigationService.Navigate(new Uri(newUrl, UriKind.Relative));
in Page2
//Temporarily hold the value got from the navigation
string textBoxValue = "";
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
//Retrieve the value passed during page navigation
NavigationContext.QueryString.TryGetValue("text", out textBoxValue)
}
private void button1_Click(object sender, RoutedEventArgs e) {
MessageBox.Show(textBoxValue);
}
Here are a number of useful links..
How to pass the image value in one xaml page to another xaml page in
windows phone 7?
How to perform page navigation on Windows
Phone

There's a number of issues. You say you have that _beseda() function on Page1, but you reference Page2() in button1_click(). Additionally, if I assume you meant Page1 in button1_click(), you're creating newPage1 and then you ask it for the text box's text.... so of course it's empty. You haven't put anything in it.
Even if you meant to put Page2 there instead, the problem is still the same.

Related

Passing TextBox Text to Navigated Page

I am trying to pass data from a textbox in one page to a text block in the navigated page. I have some code but I am finding an error when running it here is my coding.
From the page I want to send the data from:
private void button1_Click(object sender, RoutedEventArgs e)
{if (txtSID.Text != null)
{
string StudentID = txtSID.Text;
var url = string.Format("/BookingConf.xaml?StudentID={0}", StudentID);
NavigationService.Navigate(new Uri(url, UriKind.Relative));
}
Code from the Navigated Page:
protected override void OnNavigatedTo(NavigatingEventArgs e)
{
String StudentID;
if (NavigationContext.QueryString.TryGetValue
("studentID", out StudentID))
{// load event data, and set data context
ReferanceST.Text = StudentID;
}
}
The issue is that when I run the application I get an error on the 'OnNavigationTo(NavigationEventArgs e)' saying no suitable method found to override.
In order to fulfil this i placed the 'if' statement but it made no difference.
Please help me resolve this issue. Thank you.
The OnNavigatingTo takes the NavigationEventArgs, not the NavigatingEventArgs.
Change your line to:
protected override void OnNavigatedTo(NavigationEventArgs e)
The error is happening because you miss named the override method.
The error is the smoking gun
"no suitable method found to override"
To fix this
protected override void OnNavigationTo(NavigatingEventArgs e)
{
should be
protected override void OnNavigatedTo(NavigatingEventArgs e)
{
MSDN Reference

c# web browser url update

I have created a web browser in c#
this was what i get when i opened my web browser and typed google. Then i searched google for something
the result was like this
But the url wasn't updated in address bar. How to update the address bar when user click on a link on any website in my web browser
In the first image the url was google.com
In the second image url was https://www.google.co.in/#hl=en&output=search&sclient=psy-ab like that some thing but it wasn't updated
You must update the textbox on top with the URL of the WebBrowserControl, using the webBrowser1_Navigating event.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
textbox1.text = webBrowser1.Url.ToString();
}
Check http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser_events.
I think you can use Navigating event to detect when user starts search or navigates to another page.
The Form_Load must contain this:
private void Form1_Load(object sender, EventArgs e)
{
web = new WebBrowser();
web.Navigated += web_Navigated;
}
and this function:
private void web_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
textBox1.Text = web.Url.ToString();
}

How can i open up a simple page from my panorama app?

I have a panorama app and made a button on one of the pages. How can open up a page when the user clicks the button?
private void button1_Click(object sender, RoutedEventArgs e)
{
SearchPage sp = new SearchPage();
sp.Visibility = System.Windows.Visibility.Visible;
Now, the sp.Visibility seems to be the wrong way to go about it. Could anyone help me figuring out how I show secondary pages?
Just like web applications, in your silverlight application you need to redirect the user to the next page.
NavigationService.Navigate(new Uri("/SearchPage.xaml", UriKind.Relative);
you can pass parameters to your page by adding query string parameters:
NavigationService.Navigate(new Uri("/SearchPage.xaml?name=" + txtName.Text, UriKind.Relative);
and in SearchPage.cs you can retrieve the query string by saying this:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
var name = NavigationContext.QueryString["name"];
}
When you say a page, you mean a panoramaitem or a new page in your application?

How can I navigate and pass data between Pages?

I'm a bit of a beginner with this so i'll try and keep it simple.
I have a a xaml page with a button click event linking it to another xaml page. What I'm trying to do is on the click event take two strings and pass them to a text box on the second page. Can you please show me a simple code example of how to do this?
On the button click event of the first page you do something like the following
private void button1_Click(object sender, RoutedEventArgs e)
{
string urlWIthData = string.Format("/Page2.xaml?name={0}", txtName.Text);
this.NavigationService.Navigate(new Uri(urlWIthData, UriKind.Relative));
}
On the desintation page, you do the following:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
myTextBox.Text = this.NavigationContext.QueryString["name"].ToString();
}

Making two boxes equal each other

I am trying to make a text box (UPC_txtBox4) self populate to equal the same value of UPC_txtBox2. The two text boxes are on separate forms but I feel there should be a way to link the two.
If form1 is responsible for navigating to form2, then you can pass the value on the query string from form1 using a URL similar to the following:
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
Response.Redirect(Request.ApplicationPath + "/Form2.aspx?upc=" + UPC_txtBox2.Text, false);
}
}
then in form2 code:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// Assuming this field is an asp.net textbox and not an HTML input
UPC_txtBox4.Text = Request.QueryString["upc"];
}
}
Alternatively, you could store the value in session state, assuming that you are using sessions.
CORRECTION: Seeing as you are using WebForms, not WinForms as I had assumed, the below is irrelevant. I'll leave it just incase it helps someone else.
You should just create a method on the form that needs to be updated, then pass a reference when of that form to the newly created form.
This won't work if either form is a dialog (as far as I know).
So:
Form that has the textbox that will be directly edited.
private Form formToUpdate;
public void OpenForm(Form _formToUpdate)
{
formToUpdate = _formToUpdate;
txtBlah.TextChanged += new EventHandler(OnTextChanged);
this.Show();
}
private void OnTextChanged(object sender, EventArgs e)
{
formToUpdate.UpdateText(txtBlah.Text);
}
Form that is to be dynamically updated:
delegate void StringParameterDelegate (string value);
public void UpdateText(string textToUpdate)
{
if (InvokeRequired)
{
BeginInvoke(new StringParameterDelegate(UpdateText), new object[]{textToUpdate});
return;
}
// Must be on the UI thread if we've got this far
txtblah2.Text = textToUpdate;
}
Note: this is untested (although it should work), and largely pseudo code, you'll need to tailor it to your solution obviously.

Categories