ASP.NET – How Do Delegates Fit In?

asp.netdelegates

A developer told me they used delegates to bind most of their events in ASP.NET. Until then, I did not even know it was possible to use delegates in ASP.NET in a meaningful way.

My understanding is that ASP.NET/MVC3 works via HTTP verbs, not events/delegates. Is it even possible to have an event fire from the client to the server? This strikes me as an invalid form of IPC.

Best Answer

Delegates are used in ASP.NET for binding the events from controls on the page to the functions in your code behind. If we take the ASP.NET button for example, we have the click action, command action, DataBinding action, along with Disposed, INit, Load, PreRender and Unload. All of those have delegates associated with them and if you create a function that does something with it, the delegate will point to your function.

I think the confusion is when you use the delegates. Since ASP.NET MVC3 is built on ASP.NET and is not its own beast. You will still use delegates when using input fields or any of the asp controls even when using MVC. Like maple_shaft says:

Everytime there is a postback to the server, the ASP.NET lifecycle events are fired in order, causing the server side objects to be created, updating the viewstate with the updated information from the client, updating the server side elements with data from the viewstate, executing any server side events that need to be executed, and finally rendering the HTTP response to return back to the client.

The way that the events are executed is through the use of delegates.