Passing TextBox Text to Navigated Page - c#

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

Related

ViewState value not being retained during postback

How can I make this piece of code work? I am dealing with a bigger issue but if I can make this work then I will know what to do.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Response.Write(ViewState["Value"].ToString());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["Value"] = "Button clicked";
}
Page_Load event happens before Button1_Click and hence you wont be able to access a value that is not already set.
You will need to use an event that happens after Button1_Click like Page_PreRender as you have used in the answer.
Please go through this link to understand Page Life Cycle, which is invaluable in Asp.Net Webforms development.
I was able to solve my problem by putting my logic in pageLoad method in the page_PreRender method like this:
protected void Page_PreRender(object sender,EventArgs e)
{
if (IsPostBack)
{
Response.Write(ViewState["Value"].ToString());
}
}

C# how to stop a control's event

I have an .aspx page that has a page_load as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.User.Identity.Name != "")
{
....
}
else
{
FormsAuthentication.RedirectToLoginPage();
return;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
....
}
The issue I am noticing is that when the user clicks the submit button it goes though the Page_Load (goes though the else) and then tries to run though the protected void btnSubmit_Click(object sender, EventArgs e).
Is there a way to make it redirect to the login page and not continue to the next void?
This is NOT an issue. This is the normal ASP.NET Page Life Cycle.
Controls events fire right after the Page_Load event.
Note that the FormsAuthentication.RedirectToLoginPage method does not end the request by calling HttpResponse.End. This means that code that follows the RedirectToLoginPage method call will run.
Assuming you want to stop further processing from within the Page_Load (which I believe is your intention with return):
protected void Page_Load(Object sender, EventArgs e)
{
if (/*I want to kill processing*/)
{
// Method one:
Response.End(); // Though admittedly ugly
// Method two:
this.Context.ApplicationInstance.CompleteRequest();
return; // return as normal (short-circuit)
}
}

control not found message is displayed

Error Message displayed as "AddFavoriteRadWindow not found"
My code:
protected void btnAddReport_Click(object sender, ImageClickEventArgs e)
{
this.form1.Controls.Add(AddFavoriteRadWindow); // working fine
}
protected void btnOk_Click(object sender, EventArgs e)
{
if (txtReportFavorite.Text != string.Empty)
{
// code for inserting into db..
AddFavoriteRadWindow.Visible = false; // not working
}
}
"AddFavoriteRadWindow not found" message is displayed when I want to hide the rad window
You need to get the instance of your added controls from your Control Collection. Try
(this.form1.FindControl(AddFavoriteRadWindow.ID) as RadWindow).Visible = false;
You may put a check in place against null. Something like.
if((this.form1.FindControl(AddFavoriteRadWindow.ID) as RadWindow) != null)
(I am not sure about your class name, I have used RadWindow but you can replace that with your class name)
EDIT: You should pass the string id of the control in your FindControl method to get that specific control back

using sessions variable

In my master page, I'm loading a variable in the session like this:
public partial class TheMasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewUserPreferences SessionUserPreferences = new ViewUserPreferences();
SessionUserPreferences = UserPreferences.GetUserPreferencesFromDB(6);
}
}
}
Then, in the code behind of a file, I have this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var test = Session["SessionUserPreferences"];
}
}
But when I debug, test is null. What's causing the problem?
Also, if I put a break point in the master page, it doesn't trigger when I run the aspx page; is this normal?
Thanks.
First thing you are missing the assignment part for UserPreferences.GetUserPreferencesFromDB(6) to the Session object. (I read the comments for #Greg's answer and you mentioned that even after that it is not working.)
Second, Master Page's Page_Load Event is triggered after the Current Page's Page_Load Event, hence the value of Session["SessionUserPreferences"] is null in Current Page's Page Load event since it is not set yet.
Check this link for further information on Page Events:
http://msdn.microsoft.com/en-us/library/dct97kc3.aspx
You have to do Session["SessionUserPreferences"] = something; somewhere before you attempt to retrieve that. Are you setting it somewhere else that you didn't show?

Null Reference exception when accessing property in OnClick

Please help me to figure out what is wrong with this code:
I have ASP.NET page with one button visible.
When user clicks it - it instances MyClass (its implementation is in AppCode directory) and turns invisible (button2 becomes visible).
When I click button2 it raises "Object reference not set to an instance of an object" Exception.
What seems to be the problem?
{
public MyClass noviTest;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
noviTest = new MyClass(TextBox1.Text);
Button1.Visible = false;
Button2.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text=noviTest.getID; //this is the critical line
}
}
Since on postback the noviTest reference to MyClass is not recreated.
You can add it to the viewstate to keep a reference to it. (Assuming MyClass is serializable).
In Button1_Click:
ViewState("noviTest") = noviTest;
Then in Button2_Click:
var noviTest = ViewState("noviTest");
noviTest is null inside Button2_Click.
Try something like this:
protected void Page_Load(object sender, EventArgs e)
{
noviTest = new MyClass(TextBox1.Text);
}
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Visible = false;
Button2.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = noviTest.getID;
}
This will cause noviTest to be instantiated on each page request, regardless of which button was clicked. This may not be the best solution for your particular application (I am unfamiliar with the rest of the logic in this class) but you get the idea.
Each visit to the code-behind is kind of like running the code from scratch, nothing is set up or preserved for you between visits to page.
So when you hit the second button noviTest is not initialised and therefore when you attempt to call .getID to you get a null reference exception.
To deal with this you need to ensure that noviTest is initialised. If you want to have it persisted between visits to the page you need to use some mechanism to either store or recreate the instance. To store it you would (probably) use session. To recreate you would use either session or viewdata to persist a key value that would then allow you to retrieve the object state from storage of some kind.

Categories