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
Related
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
I have website with array (list) of 1000 objects, these objects are loading from json to array every website refresh. I would like to load these objects from json to array only once and keep it in RAM for others users. Because everytime read file is much slower than read it from RAM.
I am using ASP.NET Web Forms
How is it posssible?
I would recommend to define the array as an static member of a class and then initialize it with help of Global.asax, use the Application_Start event handler.
to add Global.asax to you project in Visual Studio:
File -> New -> File -> Global Application Class
Here is a sample C# code for Global.asax.cs:
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
// ... Your initialization of the array done here ...
}
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)
{
}
}
Are these values static, i.e., do they stay constant while your application is running? In that case, the easiest way is to cache those values.
You can use static variables for that, but the recommended way is to use the thread-safe Cache object provided by ASP.NET. It can be accessed with the Cache property of the Page or of the HttpContext.
Example:
var myList = (MyListType)Cache["MyList"];
if (myList == null)
{
myList = ...; // Load the list
Cache["MyList"] = myList; // Store it, so we don't need to load it again next time.
}
Further reading:
Caching Application Data
How to refresh another user control declared in a ascx page from button click event of that page(ascx.cs)?
You can load it dynamically.
First from page load event
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostback)
{
LoadControl();
}
}
And then load it from a button again on which you want to reload it
protected void Button_Click(object sender, EventArgs e)
{
LoadControl();
}
private void LoadControl()
{
usercontrols_SimpleControl ucSimpleControl = LoadControl("~/UserControlName.ascx") as usercontrols_SimpleControl;
Placeholder1.Controls.Add(ucSimpleControl);
}
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 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)
{
}