Types of Server Controls ASP.NET Web Server Controls HTML Server Controls Creating Server Controls Dynamically
Aug 09, 2015
Types of Server Controls
ASP.NET Web Server Controls
HTML Server Controls
Creating Server Controls Dynamically
With ASP. Net, these are any controls that run in the server space.
o Handled by ASP. Net runtimeo Must have runt=“server” attribute within the ASP. Net web formo Always identified with the “ID” attribute (also mandatory with
ASP. Net web controls)
Properties, events, etc. can all be handled by server-side code
• Label• Textbox• Button• Link• Link Button• Dropdown List• List Box• Checkbox• Radio Button• Image• File Upload• Panel
• <span />• <input type=“text” />• <input type=“submit” />• <a href=[#|page] />• <a href=[postback] />• <select />• <select [size=x] />• <input type=“checkbox” />• <input type=“radio” />• <img />• <input type=“file” />• <div />
• Like the ASP. Net Page object (System.Web.UI.Page), web controls maintain the same event processing– Init, PreRender, Load, etc
• If AutoPostBack is set on the control, the event will be fired immediately (causes an automatic postback), otherwise, the event will fire upon next page postback• Checkbox
– Check Changed()• Button
– Click(), Command()• Dropdown List
– Text Changed(), SelectedIndexChanged()
• System.Web.UI.HtmlControls– Ex: HtmlButton, HtmlTable, HtmlInputText
• These are ASP. Net HTML server controls– Rendered as:– <button />, <table />, <input type=“text” />
• The HtmlControls collection allows the programmer access to HTML elements (and their properties, etc.) from server-side code
– Hence, page content (HTML elements) can also be dynamically generated in the code behind or classes
• All controls in this collection inherit from the HtmlControl class
• HtmlAnchor• HtmlButton• HtmlInputButton• HtmlInputHidden• HtmlInputText• HtmlImage• HtmlSelect• HtmlTable• HtmlTextArea• HtmlGenericControl
<a /><button /><input type=“button” /><input type=“hidden” /><input type=“text” /><img /><select /><table /><text area /><span />, <body />, <h1 />,
<div />…
• Properties and Events can be managed efficiently in
the code behind
• Any server control can be referenced by its ID
property
Example:
txtTextBox.Text = “Hello!”;
• Code is written by the event handler that is
defined by the control
– <asp: Button On Click=“bttnGo_Click”... />
bttnGo_Click(object sender, …) { … }
• This is the key to maintaining clean, efficient
code for processing form data and UI
interaction
• It is good practice to prefix the name of control ID to text that indicates its “Type”
o Textbox txtNameo Checkbox chkCorrecto Dropdown List ddlProductTypeso Panel pnlCustInfo
• This will make it easier when referring to the controls at runtime in code
• Data– GridView– DataList– SqlDataSource– etc.
• Validation (server-side)– RequiredFieldValidator– RangeValidator– RegularExpressionValidator
• Navigation– SiteMapPath– Menu– TreeView– etc.
• Login– Login– LoginView– etc.
• Everything in VB/C# is an object, therefore
ASP. Net server controls are objects as well
• Controls can be dynamically created using
server side code (VB/C#)
Control Name Properties
RequiredFieldValidator
CompareValidator
RangeValidator
RegularExpressionValidator
CustomValidator
ValidationSummary
ControlToValidate, Display , ErrorMessage,ValidationGroup,ControlToCompare,
MaximumValue,MinimumValue
Validation Expression
Text,ClientValidationFunction, OnServerValidateHeader Text