Let's say I have a base page class with a Page_load() event handler and I also have a Page_load() event handler in the derived page class. I realised you don't have to wrote vitual or override keyword and compiler does not detect error. Why is this? And why is is base class version executed?
Those are just events.
To override, you need to use the OnLoad method available on Control.
Try overriding the event firing method and call your own. I can't explain why it's not complaining about both but I know they are added to the event, and hence are both in the queue to be executed.
protected override void OnLoad(EventArgs e)
{
this.Page_Load(this, e);
}
The method you are overriding here doesn't do anything fancy except execute all event handlers in the queue. See the snippet from Reflector below:
protected internal virtual void OnLoad(EventArgs e)
{
if (this.HasEvents())
{
EventHandler handler = this._occasionalFields.Events[EventLoad] as EventHandler;
if (handler != null)
{
handler(this, e);
}
}
}
I was wrong. There was indeed a warning about shadowing a parent class method, but it was just a warning, not an error.
Related
I've run into a weird problem while developing a control. I've registered a handler for Page.InitComplete event from the control, but the handler is not executed. I need the handler to be executed exactly on InitComplete event, because I depend on complex application architecture.
My code looks like this (This code is placed inside my control class):
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Page.InitComplete += (sender, args) => OnInitComplete(args);
}
However OnInitComplete is never entered.
If it is somehow possible I would like to find the way to register my handler to Page.InitComplete event without using my own events & weird hacks.
Apart from that I'm curious about the reason of this behaviour.
I'm sorry - my fault. The problem was in custom PageBase class :
protected override void OnInitComplete(EventArgs e)
{
if (!String.IsNullOrEmpty(FormAction) && FormActionInitComplete != null)
{
FormActionInitComplete(FormActionSender, FormAction, FormActionValue);
}
}
The Page.OnInitComplete method is implemented in the following way :
protected virtual void OnInitComplete(EventArgs e) {
EventHandler handler = (EventHandler)Events[EventInitComplete];
if (handler != null) {
handler(this, e);
}
}
and in the overriden method of the PageBase there was no call of the base.OnInitComplete method - that is why the event was not raised.
I have C# Web Application that has an aspx page hosting a user control (Review.ascx). Inside that user control there are 5 more user controls, one of which has a public event (Review_Summary.ascx). The problem is no matter what i do I cannot get the event wired up in the parent ascx control (Review.ascx).
Here is what I have in the child control (Review_Summary.ascx)
public event EventHandler forwardStatusChanged;
#region methods
protected void btnForward_Click(object sender, EventArgs e)
{
if (btnForward.Text == "Return")
{
if (forwardStatusChanged != null)
{
forwardStatusChanged(sender, e);
}
removeForward();
}
}
In the parent control (Review.ascx) I have this
public void initReview(string EmployeeNumber)
{
RevSummary.forwardStatusChanged += new EventHandler(RevSummary_forwardStatusChanged);
<more code here>
}
protected void RevSummary_forwardStatusChanged(object sender, EventArgs e)
{
lblReadOnly.Visible = false;
}
RevSummary is the ID of the child control in the parent control. InitReveiw is a method that is called by the aspx page in its Page_Load event.
I get no errors on compile or at runtime. But when I click the button the forwardStatusChanged event is null. The "removeForward()" method that is called after that executes properly. So that fact that the event is always null leads me to believe that the wire up in the parent control is not working. However, I am sure it is executing becasue all of the code after that executes.
How can I figure out why this event is not wiring up?
Where is initReview being called from? Are you sure it's being called because the only reason this happens is that the event handler wasn't truly setup. I've never found a reason other than this, the several times I did this myself.
HTH.
I need to use the mentioned 3rd party datepicker and it throws an exception when an invalid date it entered. The author only exposes one event, which is fired when a successful parse takes place. How, in ASP.NET could I catch this error and do something about it, like set a label's text?
There are a couple of approaches you can take here, personally I would replace the default event handler for the TextChanged event via inheritance.
The code assigns one via during the setup and unfortunately textbox is a private member
textBox.TextChanged += new EventHandler(OnSelectedDateChanged);
which is declared as
protected virtual void OnSelectedDateChanged(object sender, EventArgs e)
So we can inherit SlimeeLibrary.DatePicker
public class EnhancedDatePicker : SlimeeLibrary.DatePicker
and then override the EventHandler raising a new parse error event.
public event EventHandler OnDateParseError;
protected override void OnSelectedDateChanged(object sender, EventArgs e)
{
try
{
base.OnSelectedDateChanged(sender, e);
}
catch (FormatException fe)
{
OnDateParseError(sender, e);
}
}
Hope that helps. I haven't checked it but have examined the code for slimees control, but don't want to setup a code project account to download it sorry. You'll obviously need to change your ASP.NET usercontrol references to use the new class.
I need the following functionality in my method: if the method is called before OnLoad event of ASP.NET life cycle throw an exception else continue execution of the method.
I was thinking of something like this:
if (Page.LifeCycleState < LifeCycleState.OnLoad) {
throw new InvalidPageStateException();
}
Is it possible to retrieve the state of ASP.NET page life cycle?
One approach would be to use a Basepage that you always use in your site. This would contain a variable called PageLoadComplete, which you would set at the end of your PageLoad event. Then you could check the state of this variable from within your method.
public abstract class BasePage : System.Web.UI.Page
{
public bool PageLoadComplete { get; private set; }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
PageLoadComplete = true;
}
}
If you want to access the variable from code external to your page such as a UserControl, you would have to make it public and cast your page as BasePage.
public partial class MyUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
BasePage basePage = this.Page as BasePage;
if (basePage != null && !basePage.PageLoadComplete)
{
throw new InvalidPageStateException();
}
}
}
There is property in a realization of System.Web.UI.Control class(realization):
internal ControlState ControlState {
get { return _controlState; }
set { _controlState = value; }
}
Where ControlState is enum that contains members such as: Initialized, ViewStateLoaded, Loaded etc. here declaration
But as you can see this property is internal. So only way to get control state is proposed by Daniel Dyson.
You maybe able to find what you are looking for, by looking at the CurrentHandler and PreviousHandler properties of the current HttpContext.
if the method is called before OnLoad event of ASP.NET life cycle
throw an exception else continue execution of the method.
It is not clear which Onload event is meant, nor where the "method" resides. Is it the Page's Onload or a Control's OnLoad? Is it a Page's "method" or a Control's "method"?
Anyway, one can store sort of flag in the Context.Items Dictionary, which all controls (including Page) have access to during a request. This eliminates the need to use a general base page like suggested obove.
In the OnLoad method (no matter whether it is a Page's OnLoad or a Control's OnLoad):
Context.Items[UniqueID] = this;
In the "method":
if (Context.Items[UniqueID] != null)
{
throw new InvalidPageStateException();
}
I have a custom PageBase class that I am using for all of the pages in my project. In my BaseClass I have a protected override void OnLoad(EventArgs e) method declared. This method seems to make my derived classes unable to throw their own OnLoad event. What is the preferred way of making both OnLoad events able to work?
In your derived class:
protected override void OnLoad(EventArgs e)
{
//do the specific work
//....
//
base.OnLoad(e);
}
Not sure why this wouldn't work. Are you overriding OnLoad in your derived classes or just hooking up to the Load event? If you are just hooking up to the load event are you sure your base classes OnLoad event is calling base.OnLoad()?