I am regularly encountering errors of type "An unhandled exception of type 'System.StackOverflowException' occurred in Unknown Module.". This happens in a website with a quite large code base. But the error occurs only after several minutes of the website.
This is where the error pointed me to:
public partial class HealthInsurance : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
base.OnLoad(e);
Page.Header.DataBind();
}
}
You should not call the base class' implementation of OnLoad() from an autowired Page_Load() handler.
Under the hood, OnLoad() is responsible for invoking Page_Load(), so your code ends up in an infinite recursive loop.
You only have to write:
protected void Page_Load(object sender, EventArgs e)
{
Page.Header.DataBind();
}
Things would be different had you chosen to override OnLoad() rather than rely on Page_Load(). In that case, you indeed have to call the base class' method:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Page.Header.DataBind();
}
If you are subscribing to the Load event, do not call the base OnLoad as the base OnLoad is what's responsible for firing the Load event, so it will be an endless cycle.
However, if you are overriding the OnLoad method, then it's appropriate to call the base OnLoad method, e.g.
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Page.Header.DataBind();
}
In the above case, note the override keyword.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With