How do I associate "event code" with a Web control that I've placed on a Web Form? Can I always just double-click the Web control in the Design view?


In this hour we saw how to have the Button Web control's Click event associated with an event handler provided in the source code section. One way to accomplish this is by simply double-clicking the Button Web control in the Design view. This causes Visual Web Developer to inject some code into the page's source code portion. For the Button Web control, a Click event handler is added.

However, a Web control may have a number of different events, meaning we can, potentially, have numerous event handlers in our ASP.NET page's source code. Each Web control has a default event. When the Web control is double-clicked in the designer, an event handler is created for this default event. (Note that the Button Web control's default event is the Click event.)

Adding an event handler for an event other than a Web control's default event requires that you go directly to the source code portion and pick the Web control and event from the two drop-down lists at the top. This is a technique we discussed briefly in both this hour and in Hour 2.


What would happen if I placed the financial calculation code in the Page_Load event handler instead of the Button_Click event handler?


Recall that the source code in the Page_Load event handler executes every time the ASP.NET web page is requested. When the user visits the page for the first time, she has yet to enter the loan principal, interest rate, and duration. Therefore, in attempting to compute the calculation, we will get an error. Specifically, the code will try to perform numerical calculations on a user-inputted value that has yet to be provided.

Because we want to perform the calculation only after the user has provided the required inputs, the source code for the calculation is placed in the Button's Click event handler.

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows