Pass value from one xaml page to another xaml page in silverlight - asp.net

Hello I am very new to Silverlight and I want to pass value from one Xaml page to another Xaml page in Silverlight. I got some solution for that as
protected void btn_click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Page1.xaml?key1"=txtname.Text, UriKind.Relative));
}
but I am finding an error in it as
An object reference is required for the non-static field, method, property System.Windows.Navigation.NavigationService.Navigate(System.Uri)'

Syntax issue. unless that is a typo
protected void btn_click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Page1.xaml?key1"=txtname.Text, UriKind.Relative));
}
should change to...
protected void btn_click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Page1.xaml?key1="+txtname.Text, UriKind.Relative));
}
Note the change from
"/Page1.xaml?key1"=txtname.Text
to
"/Page1.xaml?key1="+txtname.Text

Related

Base Page/Derived Page event model in ASP.NET

I have written the following code in ASP.NET
I have a base page:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Base Page Called");
}
I have a derived page which have following code:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Derived Page Called");
}
Now while I am calling the Derived page it doesn't call Base Page's Page_Load. It displays
"Derived Page Called".
Now if I change the Derived page Load event handler name to "Page1_Load" and the implementation as following, the Base page is called.
protected void Page1_Load(object sender, EventArgs e)
{
Response.Write("Derived Page Called");
}
"Base Page Called".
What is the reason for this kind of behaviour?
Page_Load is automatically wired up if there exists a method with the Page_Load name, so if you define one in the derived class it will hide the one from the base. However, it has to match by name, so by giving the one in the derived class a suffix, it no longer hides the base implementation, so it will pick up the base one and use it.
If you put Page1_Load in the base as well, you will get no output
Base:
protected virtual void Page_Load(object sender, EventArgs e)
{
Response.Write("Base Page Called");
}
Derived:
protected override void Page_Load(object sender, EventArgs e)
{
base.Page_Load();
Response.Write("Derived Page Called");
}
Try the above if you want both to be called

How to know the previous event handled in asp.net

on page load i have shown data from database onto a gridview(with edit and delete option). I also have a search button on the page. when i click on search the searched data should be visible in the gridview, which is working fine. But when i click on delete link after search it does not take the searched row. The page postsback after clicking on delete. I need to check the event performed prior to event performed on gridview. How to do that?? I hope that the issue is clear.. Any help is appreciated. Thanks.
Page_Load occurs before RowCommand, are you only binding grid if(!IsPostBack) ?
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
protected void SearchButton_Click(object sender, EventArgs e)
{
BindGrid(searchText.Text);
}
protected void GridView_RowCommand(object sender, GridViewRowEventArgs e)
{
switch(e.CommandName)
{
case "Delete":
DeleteRow(e.Row.CommandArgument);
break;
}
}
private void BindGrid()
{
//GridView binding business logic.
}
private void BindGrid(String searchTerm)
{
//GridView binding logic with searchTerm.
}
private void DeleteRow(String commandArg)
{
//Convert command arg to ID or whatever and delete data.
}

ASP.NET Profile

I have this:
protected void Page_Load(object sender, EventArgs e)
{
nome_Txt.Text = Profile.dados_pessoais.nome;
}
protected void save_Click(object sender, EventArgs e)
{
Profile.dados_pessoais.nome = nome_Txt.Text;
}
If Profile.dados_pessoais.nome is empty, nome_txt.Text is empty too. When I change nome_Txt.Text to teste for example, when I click on the button nome_Txt.Text is empty.
What am I doing wrong?
The Page_Load event run before the button click event so you always assign the text box to empty value.
To solve this, don't populate the textbox when you are in a Postback:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
nome_Txt.Text = Profile.dados_pessoais.nome;
}
}
As also stated in a comment, you probably have to save the profile after changing it otherwise it won't be saved when you next load the page:
protected void save_Click(object sender, EventArgs e)
{
Profile.dados_pessoais.nome = nome_Txt.Text;
Profile.Save()
}

How to get event in another control event?

In my application, i am trying to get button_click event in another button click event, but it is not working.
My code is,
protected void btn1_Click(object sender, EventArgs e)
{
Response.Write("hi.....");
}
protected void btn2_Click(object sender, EventArgs e)
{
btn1.Click += new EventHandler(this.btn1_Click);
}
What is wrong with this?
I suppose you want to call the code of btn1_click event.
If this is the case, you simple call it as a method in your btn2_Click.
protected void btn2_Click(object sender, EventArgs e)
{
btn1_Click (sender, e);
}

How does Global.asax PostAuthenticateRequest event binding happen?

How can I use the PostAuthenticateRequest event of Global.asax? I'm following this tutorial and it mentions that I have to use the PostAuthenticateRequest event. When I added the Global.asax event it created two files, the markup and the code-behind file. Here is the content of the code-behind file
using System;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace authentication
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
Now when I type the
protected void Application_OnPostAuthenticateRequest(object sender, EventArgs e)
It is successfully called. Now I want to know how is the PostAuthenticateRequest bound to this Application_OnPostAuthenticateRequest method? How can I change the method to some other?
Magic..., a mechanism called Auto Event Wireup, the same reason you can write
Page_Load(object sender, EventArgs e)
{
}
in your code-behind and the method will automatically be called when the page loads.
MSDN description for System.Web.Configuration.PagesSection.AutoEventWireup property:
Gets or sets a value indicating whether events for ASP.NET pages are automatically connected to event-handling functions.
When AutoEventWireup is true, handlers are automatically bound to events at run time based on their name and signature. For each event, ASP.NET searches for a method that is named according to the pattern Page_eventname(), such as Page_Load() or Page_Init(). ASP.NET first looks for an overload that has the typical event-handler signature (that is, it specifies Object and EventArgs parameters). If an event handler with this signature is not found, ASP.NET looks for an overload that has no parameters. More details in this answer.
If you wanted to do it explicitly you would write the following instead
public override void Init()
{
this.PostAuthenticateRequest +=
new EventHandler(MyOnPostAuthenticateRequestHandler);
base.Init();
}
private void MyOnPostAuthenticateRequestHandler(object sender, EventArgs e)
{
}

Resources