What is the purpose of raising an event? - asp.net

what is the advantage of raising and event in asp.net user controls?

To allow someone else to execute code when you do something, without having to poll your state to know what you've done.

The purpose of raising an event is typically to inform about something that has happened or is about to happen in the code, in order to allow other parts of the program to react on this. Usually the events are designed in a way so that the code that raises the event works the same way regardless of whether there are any listeners (event handlers) attached or not.
One example would be the click event of a button. When the user clicks the button, the button will raise a click event, which will allow any attached event handler to do something. But if there is not event handler attached, the button will not change its behavior (it's just that nothing will happen).
Though most events are raised to inform that something has happened, there are some events that are raised in order to inform that something is about to happen. Sometimes these event use an EventArgs class with read/write properties (as opposed to readonly properties which are more commonly found in EventArgs classes) which allows the event listener to communicate data back to the event-raising code. I can't come up with any good asp.net example from the top of my head, but in the winforms world a typical example of this would be the Form.Closing event, where the event handler can set e.Cancel = true to prevent the form from closing.
So, in a user control it would be useful to raise an event whenever it may be of interest for external code (typically the page) to react on something that happens within the control.

To add to the other answers already here, let me use an analogy to explain events.
Suppose you wish to receive newspapers each day. You telephone the newspaper company to inform them that you wish to receive any new newspapers they may print - you 'subscribe' to the newspaper. The newspaper delivery people maintain a list of people who are subscribers - people who should receive the paper. When the newspaper is printed each day, if you are on the subscriber list, you will receive a newspaper.
Now, suppose you have an object - a button, for instance. Suppose you want to know when that button is pressed. You 'subscribe' to events - specifically an 'OnClick' or 'OnPressed' or whatever it may be named in your language of use. Whenever the user clicks the button, the button goes through its list of subscribers, and calls the function supplied to each. These are the 'event handlers'. These functions are what the subscribers want to have called when the event occurs. In English, the subscriber might say "When you're pressed, call MyOnClick() function."
Events are used in many programming paradigms to cope with complexity - events do not need to know anything about event handlers, and vice versa. This allows for looser coupling, and more modular, reusable code.
I suggest you read about the Observer Pattern, as this is the foundation for events and event handlers.

Related

Is there a way to track any form being filled on google tag manager?

Is there a way to track any form being filled on google tag manager? I looked at the options, and there doesn't seem to be a way to do this, I can capture events for form submission, but not when the form is being filled by an user. Is there an easy way to do this? I tried to capture the event with input being clicked by the user, but it triggers on every page load. Check if one of the input field in the form is being filled.
Well, you generally have two ways of doing it: ask your front-end devs to write the code and just send you a dataLayer event when it happens, or write the code on your own.
If you're not able to delegate it to the FE devs, then you will have to write it on your own. Consider what you want to do. The simplest thing from here would be detecting clicks on that one field, assuming that if a user clicked it, then they will fill it. Tracking like this is not perfect since there are other ways to fill a field without clicking/tapping it And also there will be cases when a user clicks it, but doesn't type anything. But this is probably what you want to do. For this, you only need to pick a right CSS selector and use built-in funcitonality of GTM.
If you actually want to track when the field changes input, you will have to write your own event listeners and with it, send yourself dataLayer events and then listen to those and send the event accordingly.
This is what you want to know about the change event: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event
You will also likely want to prevent event sent on every change, so make a blocking variable that you set after the listener callback fires once.
This is very messy. It's not likely worth it to go this route.

FullCalendar: is there still a viewRender event hook?

I need something like the viewRender event in order to persist the user's state. I'm building a UI where users will frequently jump in and out of the calendar, so preserving their view/range is essential for a pleasant experience. Does this exist in v5? The last mention I can find of it is from v3.
The only workaround I can think of right now is a direct click handler on every view control element, or a very heavy-handed MutationObserver. This is a React app so either one is going to be super awkward.
Thank you!
Edit 2021-02-11:
I looked at the available view render hooks but none of them address my problem. What I need is an event that will fire whenever the view state changes, including clicking between weeks/months/etc., so that I can persist the date range the user most recently viewed as well as the view they had selected.
viewDidMount is the closest to what I need, but it does not fire when the date range changes.
Edit 2021-05-26:
Another problem with using viewDidMount is that using it to enact side-effects is a bit overeager. The hook gets called whether or not the user has actually done anything, and the default view always gets passed as view inside the View Object. So there's no way to tell whether this mount event contains data I should persist or not.

What is the event available for canceling editing?

I want to do something when the author opens a page, makes some changes and closes without saving.
I am expecting an event like close or cancel, but I can't find any event like that.
Which event should I subscribe to in this scenario?
There is no close or cancel event and since the user did not perform a save there is no event called at all (unless it is an existing item, then the close would trigger an undo-checkout). Please note that the user does get a warning when he tries to close a window without saving, so it is considered a conscious decision here.
Your only way to interact with this would be through a UI extension, but it would be interesting to know a bit more about the use case to see if it actually makes sense to go down that route (a UI extension is nontrivial).

ASP .NET: when UserControls loads?

Is MasterPage -> Page -> UserControl the loading order of an ASP .NET request?
Is there a situation where an UserControl loads before the Page loads ?
I have private messages for my users and in every page they will see a message like this "You have 3 unread messages."
When users view an unread message I want to change the message to this "You have 2 unread messages."
This can be easily done with a Request.Redirect, but I want to avoid this.
message 1 -> click on it will goes to MarkAsRead.aspx?id=x wich redirects me to ViewMessage.aspx?id=x
Instead of doing this I want to mark the message as read from ViewMessage.aspx?id=x then decrease my Session variable "unreadMessages", then let my UserControl display the new number.
But my concern is that UserControls may not always load last.
The event progression in ASP.NET is as follows:
Page.OnPreInit()
UserControl.OnInit()
MasterPage.OnInit()
Page.OnInit()
Page.OnInitComplete()
Page.OnLoad()
MasterPage.OnLoad()
UserControl.OnLoad()
Page.OnLoadComplete()
(page event handlers fired here)
Page.OnPreRender()
MasterPage.OnPreRender()
UserControl.OnPreRender()
Page.OnPreRenderComplete()
Page.OnSaveStateComplete()
UserControl.OnUnload()
MasterPage.OnUnload()
Page.OnUnload()
To accomplish what your trying to do, handle your message count update in your Page.Init event, then display that count during the UserControl.Load event. I've provided the full progression of events above, which may provide you more options in case the above scenario isn't adequate. (For example, you could update the count in Page.Load, and render it in UserControl.PreRender, if for some reason Page.Init was too soon.)
check this diagram for asp.net event order.
my suggestion: implement a public void UpdateInfo() in your UserControl which is called from both the UserControl's OnLoad() event and from the OnLoad() event in ViewMessage.aspx after it adjusts the counter for unread messages.
I would agree with the above answer - it can become very tricky when you rely on child user control load events. Consider how all the MS databound controls work - you need to tell them to Bind before they will perform their function.

Do I need to unsubscribe from (manually subscribed to) events in asp.net?

Do the same best practis rules regarding subscribing/unsubscribing to events apply in asp.net?
I know it might seem like a silly question, but when I think about it, I have never really seen any code where people first subscribe to an event on a page and then unsubscribe later on in the web request.
Example 1:
On a page, in the Page_Load method, I subscribe to a updating event on a ListView. Should I unsubscribe from that event later on, for example in the OnPreRenderComplete method?
Example 2:
In the passive view patter, a view (Page control/Usercontrol) will raise an event whenever it needs the presenter to do anything. So the presenter needs to subscribe to events on the view, but do it also need to unsubscribe from the events again?
Best regards, Egil.
The page instance and all of its components will "go out of scope" when request completes, e.g. they become eligible for GC. So your ListView will go out of scope along with the Page/user controls on it. You don't need to unsubscribe (unless you subscribe to an event that belongs to some kind of singleton that survives every request and use one of the methods of the page as the event handler, for example).
The same thing is valid for the presenter (again as long as this presenter is used solely with one page and goes out of scope after that).
Generally, no. Events are supposed to be dumped automatically when the page unloads. SUPPOSED to be. I've run into a bug before (in .NET 1.1) where that wasn't the case.
I won't bother unsubscribing, unless I notice a problem with the page (like, a method being called 20 times from a phantom in the call stack: that's usually a sign of something not being unsubscribed properly).

Resources