ASP.NET Web Server Controls Basic Web Server Controls
Overview The ASP.NET page framework includes a number of built-in server controls that are designed to provide a more structured programming model for the Web. These controls provide the following features: – Automatic state management. – Simple access to object values without having to use the Request object. – Ability to react to events in server-side code to create applications that are better structured. – Common approach to building user interfaces for Web pages. – Output is automatically customized based on the capabilities of the browser.
ASP.NET Web Server Controls Server controls are added to a page, and programmers write code to handle events raised by users’ interaction with the controls at runtime. Built in Server controls are divided into two groups: – HTML controls Reside in the System.Web.UI.HTMLControls namespace Map 1-to-1 with the standard HTML elements – Web controls Reside in the System.Web.UI.WebControls namespace Very similar to the Visual Basic UI controls All Web controls use the prefix asp
Web Server Control Categories Basic Web Controls – Commonly used user input controls such as labels, text boxes, and drop-down lists Validation Controls – used to validate the values that are entered into other controls of the page – perform client-side validation, server-side validation, or both List Controls – support binding to collections – display rows of data in a customized, templated format Rich Controls – task-specific controls with rich functionality – Examples: AdRotator and Calendar controls
Basic Web Server Controls Label TextBox CheckBox RadioButton DropDownList Image Panel Button HyperLink ImageButton LinkButton Literal PlaceHolder Table TableCell TableRow
Using Web Server Controls Add server control to page Set server control properties Add event handler if needed
Web Server Control Properties The System.Web.UI.WebControls.WebControl base class contains all of the common properties. Most of the Web server controls derive from this class. See the next slide for a list of common properties.
Property AccessKeyThe control's keyboard shortcut key (AccessKey). It specifies a single letter or number that the user can press while pressing ALT. AttributesThe collection of additional attributes on the control not defined by a public property, but that should also be rendered. Any attribute that is not defined by the Web server control is added to this collection. BackColorThe background color of the control. The BackColor property can be set using standard HTML color identifiers: the name of a color ("black" or "red") or an RGB value expressed in hexadecimal format ("#ffffff"). BorderColorThe border color of the control. The BorderColor property can be set using standard HTML color identifiers: the name of a color ("black" or "red") or an RGB value expressed in hexadecimal format ("#ffffff"). BorderWidthThe width of the control's border (if any) in pixels. Note This property might not work for all controls in browsers earlier than Internet Explorer 4.0. BorderStyleThe control's border style, if any. The possible values are: NotSet, None, Dotted, Dashed, Solid, Double, Groove, Ridge, Inset, Outset CssClassThe cascading style sheets (CSS) class to assign to the control. StyleA collection of text attributes that are rendered as a CSS style attribute on the outer tag of the control. Note Any style values set using style properties (for example, BackColor) will automatically override a corresponding value in this collection. EnabledMakes the control functional when this property is set to true (the default). Disables the control when this property is set to false. Note Disabling a control dims the control and makes it inactive. It does not hide the control. FontProvides font information for the Web server control that you are declaring. ForeColorThe foreground color of the control. Note This property might not work for all controls in browsers earlier than Internet Explorer 4.0. HeightThe control's height. Note This property might not work for all controls in browsers earlier than Internet Explorer 4.0. TabIndexThe control's position in the tab order. If this property is not set, the control's position index is 0. Controls with the same tab index are moved to according to the order in which they are declared in the Web page. Note This property only works in Microsoft Internet Explorer 4.0 and later. ToolTipThe text that appears when the user rests the mouse pointer over a control. Note The ToolTip property does not work in all browsers. Check with the browser for compatibility. Width The fixed width of the control. The possible units are: Pixel, Point, Pica, Inch, Mm, Cm, Percentage, Em Ex Note The default unit is pixels. Not all browsers support every unit type.
Event Model One of the key features of ASP.NET is that it uses an event-based programming model. An event handler is a method that determines what actions are performed when an event occurs, such as when the user clicks a button or selects an item from a list. When an event is raised, the handler for that specific event is executed
Event Handlers In the.NET Framework, all event handlers have a specific method signature, that is, a specific return type and parameters. Event handlers are always void methods. Event handlers always accept two parameters: – an object parameter – an EventArgs parameter (or a subclass of EventArgs, such as CommandEventArgs or ImageClickEventArgs).