Postback Architecture


A postback is an HTTP POST request (or an HTTP GET request with data in the URL query string) that occurs when a user submits a form. A control that renders a form element needs to respond to postback, capture form data, update its state, and implement other logic on postback. For the most part, you will not have to implement postback processing in your own controls because ASP.NET provides form controls such as Button , TextBox , and DropDownList that implement postback processing for the standard HTML form elements. You can easily reuse the existing ASP.NET form controls via class composition, as we will show in Chapter 12, "Composite Controls." However, in some scenarios, when performance requirements do not permit the cost of object creation that is concomitant with class composition, you might have to implement postback processing in your own control. This section provides an overview of the postback processing architecture of the page framework; the rest of this chapter shows how to implement that architecture. Although the remainder of this chapter will be useful background information for control authoring, you might want to browse through it on your first reading and return to it when you need to implement postback processing in your controls.

ASP.NET provides a simple and intuitive architecture that enables controls to perform tasks related to postback by implementing one or both of two interfaces ” IPostBackEventHandler and IPostBackDataHandler .

The IPostBackEventHandler interface enables a control to raise an event that signals that postback has occurred. In effect, this interface enables a control to map the client-side form submit event to a server-side event, which allows page developers to handle the client-side event by attaching event handlers to the server-side event. For example, the Button control raises a Click event on the server when a form is submitted. Note that a postback event, which occurs when a user submits a form, is the only client-side event that is transmitted to the server. All other client-side events ”such as key press or mouse hover events ”can be handled on the client only, as we'll discuss in Chapter 13, ­"Client-Side Behavior." A control that implements IPostBackEventHandler participates in the Raise Postback Event phase shown in Figure 9-1.

The IPostBackDataHandler interface enables a control to retrieve posted form data, update its state accordingly , and raise events on the server to signal changes in its state. For example, the TextBox control updates its state with text entered by the user and raises a TextChanged event when its state changes. A control that implements the IPostBackDataHandler interface participates in the Load Postback Data and Raise Changed Events phases shown in Figure 9-1.

Note

If your control merely needs to examine posted form data without participating in the relevant postback processing phases, it can do so via the Context property of its containing page ( Page.Context.Request.Form or Page.Context.Request.QueryString ). You have to implement IPostBackEventHandler and/or IPostBackDataHandler only when your control needs to participate in the appropriate processing phase.


In the remainder of the chapter, we'll examine how to implement IPostBackEventHandler and follow with a discussion on implementing IPostBackDataHandler . Finally, we'll develop a Login control that implements both of these interfaces.



Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net