tag. This fact ensures for the control much wider support from browsers.The HTML input button controls support the ServerClick event, which allows you to set the code to run on the server after the button is clicked. Note that if you set the button type to Button and the ServerClick event handler is specified, the control automatically adds the postback script code to the onclick HTML attribute. In this way, any click causes the page to post back and the code to execute. Let’s consider the following ASP.NET code:
onserverclick="buttonClicked" />
The corresponding HTML code is as follows:
name="btn"
type="button"
value="Click" />
The client-side __doPostBack script function is the standard piece of code generated by ASP.NET to implement the postback. If the button type is set to Submit—that is, a value that would always cause a postback—no client-side script code is generated and the onclick attribute is not set.
In ASP.NET 2.0 and newer versions, more specific controls have been added to render submit and reset buttons. The controls are HtmlInputSubmit and HtmlInputReset.
Note
The HtmlInputImage control supports a nearly identical pattern for handling server-side events and validation. The HtmlInputImage control features a few more properties specific to the image it shows. In particular, you can set the alternate text for the image, the border, and the alignment with respect to the rest of the page. The ServerClick event handler has a slightly different form and looks like the following:
void ImageClickEventHandler(object sender, ImageClickEventArgs e);
When an image button is clicked, the coordinates of the click are determined by using the X and Y properties of the ImageClickEventArgs data structure.
Controlling Validation
The HtmlInputButton class, as well as the HtmlButton class, support a Boolean property named CausesValidation. The property indicates whether the content of the input fields should be validated when the button is clicked. By default, the property is set to true, meaning the validation always takes place. We’ll examine data validation in Chapter 9. For now, it suffices to say, you can programmatically enable or disable the validation step by using the CausesValidation property.
Typically, you might want to disable validation if the button that has been clicked doesn’t perform a concrete operation but simply clears the user interface or cancels an ongoing operation. By design, in fact, server-side page validation takes place just before the ServerClick event handler is executed. Setting the CausesValidation property to false is the only means you have to prevent an unnecessary validation.
Detecting State Changes of Controls
Earlier in this chapter, while discussing the features of the HtmlTextAreacontrol, we ran into the ServerChange event and described it as the mechanism to detect and validate changes in the control’s state between two successive postbacks. The ServerChange event is not an exclusive feature of the HtmlTextArea control; it’s also supported by other input controls, such as HtmlInputCheckBox, HtmlInputRadioButton, HtmlInputHidden, and HtmlInputText. Let’s look at an example in which you use the ServerChange event to