3

During a postback, the __EVENTTARGET form variable holds the name of the control which issued the postback. How does ASP.NET know which event to fire for that control if the control supports several server side events?

LCJ
  • 22,196
  • 67
  • 260
  • 418
Tony_Henrich
  • 42,411
  • 75
  • 239
  • 374
  • are u asking WHY its doing what its doing, or HOW u can get this to work ? – brizz Apr 03 '12 at 07:57
  • None of these. If a control supports several events, how does asp.net know which to fire? Just passing the control id in __EVENTTARGET can't be enough. – Tony_Henrich Apr 03 '12 at 08:07

5 Answers5

5

As Wiktor mentions, many of the controls in ASP.Net have already been built for you to be used in certain ways; the button click, the text change, the selected index changed - these controls have been built to do certain things, which is why they work the way they do.

From the documentation:

Because most ASP.NET server control events require a round trip to the server for processing, they can affect the performance of a page. Therefore, server controls offer a limited set of events, usually only click-type events. Some server controls support change events. For example, the CheckBox Web server control raises a CheckedChanged event in server code when the user clicks the box. Some server controls support more abstract events. For example, the Calendar Web server control raises a SelectionChanged event that is a more abstract version of a click event.

You can, of course, write your own client side controls, but this requires a lot more work. The article Server Event Handling in ASP.Net discusses this. To summarise, though, the important section is implementing RaisePostBackEvent

If you want to provide multiple events, then you vary the event argument sent to this method from the client, and raise the appropriate server side event. This can be as simple as an if statement. A basic example would be having two client side javascript events, each of which might call:

__doPostBack(controlId, 'superclick');

__doPostBack(pageId, 'superchange');

And then, in your post back event handler, call the required server side event based on the passed argument. A simple RaisePostBackEvent server side handler would then look something like:

  public void RaisePostBackEvent(string eventArgument){

     if(eventArgument == "superclick")
     {
        OnSuperClick(this, new EventArgs());
     }

     if(eventArgument == "superchange")
     {
        OnSuperChange(this, new EventArgs());
     }         

  }
dash
  • 89,546
  • 4
  • 51
  • 71
  • RaisePostBackEvent contains two arguments http://msdn.microsoft.com/en-us/library/system.web.ui.page.raisepostbackevent.aspx – Pankaj Apr 03 '12 at 08:35
  • @PankajGarg That's Page.RaisePostBackEvent. See http://msdn.microsoft.com/en-us/library/system.web.ui.ipostbackeventhandler.raisepostbackevent.aspx as referenced in the answer. Alternatively, you can be lazy and just look in Page.Request.Params["__EVENTARGUMENT"] when posting back. – dash Apr 03 '12 at 08:37
2

Debugging with the aid of an HTTP sniffer such as Fiddler can help here. Broadly, the event details are in the __EVENTARGUMENT form field. How that data is unpacked is an implementation detail of the particular control.

For example, drop a TreeView onto a form, and hook up its TreeNodeCollapsed, TreeNodeExpanded, and SelectedNodeChanged events. You will see that expanding or collapsing a node results in a postback with something like tnode_text in __EVENTARGUMENT; whereas selecting a node results in a postback with something like snode_text in __EVENTARGUMENT. My guess is that t stands for "toggle", and s for "select", but I haven't dug into the source to check.

(Incidentally, just saying "toggle" is good enough for both expand and collapse, since the ..._ExpandState form variable holds details of the expand state of all nodes, it looks like).

AakashM
  • 62,551
  • 17
  • 151
  • 186
1

To handle postback ASP.NET server control must implement IPostBackEventHandler interface. This interface has a single method void RaisePostBackEvent(string eventArgument) which processes an event raised when a form is posted to the server.

This method is responsible for raising the server side events of the control. The only parameter here is eventArgument which is used to differentiate the type of event if necessary (like a GridView).

E.g. Button control raises both Click and Command events. GridView parses command name from the event argument parameter, and fires a necessary event for the command.

You could use .NET decompiler (I used excellent JetBrains dotPeek for this) to look through the implementation of RaisePostBackEvent method for the standard ASP.NET controls.

Oleks
  • 31,955
  • 11
  • 77
  • 132
0

I guess that in case of controls which hold a single value, passed in form's data, there's no need room for any doubts - TextBox can be changed, DropDownList can have its value changed etc.

In fact, such controls usually have only one event triggered from the client side, other events are for the server side pipeline (DataBinding, DataBound, Init, Load etc.)

Any composite control, on the other side, can use __EVENTARGUMENTS to provide additional parameters to the server so that the actual event can be determined and dispatched.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
0

If you would like to handle a postback event in your control, you would be Implementing IPostBackEventHandler interface. The framework would call IPostBackEventHandler.RaisePostBackEvent method which accepts eventArgument. Based on the event argument you can raise various other custom events.

Other Control events like Init, Load etc will be called by the framework automatically, irrespective of whether the control can handle postback events.

Ramesh
  • 13,043
  • 3
  • 52
  • 88