Call Event

  • Thread starter Thread starter Ignacio Machin
  • Start date Start date
I

Ignacio Machin

How can I call an event from another event:
I want to call :
DropDownList_SelectedIndexChanged
in:
Page_Load
 
This is somebody using Ignacio's machine, right?
Generally you do not "call" an event; the event is triggered by the action
of the dropdownlist having a selection changed. Maybe you could be more
clear about exactly what the goal is?
Peter
 
If you have coded an "event handler" for DropDownList_SelectedIndexChanged,
then move that code into a separate method.

Have DropDownList_SelectedIndexChanged call that method (ie. a one-liner).

Have your Page_Load call the same method.

NOTE: If the method needs "arguments" then both callers need to provide
whatever is required - this is largely one of the reasons why you do not
call event handlers manually because you would be required to provide the
Sender and EventArgs arguments, for example.

Kevin
 
Ignacio said:
How can I call an event from another event:
I want to call :
DropDownList_SelectedIndexChanged
in:
Page_Load

I'd expect the dropdownlist to actually fire that event itself if the
SelectedIndex property changed.

If, on the other hand, you want to execute the same code as changing the
SelectedIndex property does, the correct way (in my opinion) would be to
take the code out of that event handler and place it in a separate
method, ie. "UpdateRelatedData" or something, and then call that method
from both DropDownList_SelectedIndexChanged and Page_Load.

If you really don't want to do that, the "correct" way to execute the
same code would be to:

1. Call the OnSelectedIndexChanged method on the dropdownlist, if that
method is public (mostly isn't, so go to 2.)
2. Call the method you attached to the event directly, ie. call
DropDownList_SelectedIndexChanged, and provide any necessary parameter
values to it.
 
Back
Top