Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET server controls


May 13, 2021 ASP.NET


Table of contents


The server control

Controls are small blocks of functionality in the graphical user interface, including text boxes, buttons, check boxes, list boxes, labels, and many other tools. With these tools, users can enter data, make choices, and indicate their preferences.

Controls are also used for structural work such as validation, data access, security assurance, creation of master pages, and data operations.

ASP.NET uses five types of Web controls, which are:

  • HTML controls
  • HTML server controls
  • ASP.NET server controls
  • ASP.NET Ajax server control
  • User controls and custom controls

ASP.NET server control is the primary control ASP.NET used in the application. These controls can be divided into the following categories:

  • Validation Control - Used to validate user input and work by running client scripts.
  • Data source controls - Provides the ibility of binding data to different data sources.
  • Data View Control - This control is a variety of lists and tables that can display data bound from a data source.
  • Personalization Controls - Page personalization based on user information, depending on the user's preferences.
  • Landing and security controls - Provides user authentication.
  • Master page - provides a consistent layout and interface throughout the application.
  • Navigation Controls - Helps the user navigate. For example, menus, tree views, and so on.
  • Rich feature controls - Implement special features. Examples: AdRotator, FileUpload, and calendar controls.

The basic syntax for using server controls is:

<asp:controlType  ID ="ControlID" runat="server" Property1=value1  [Property2=value2] />

In addition, Visual Studio has the following features to help produce error-free code:

  • Drag and drop controls in the design view.
  • Intelligent perception of display and auto-completion features.
  • The property window that sets the property value directly.

The property of the server control

Visually enabled server controls ASP.NET the WebControl class and inherit all the properties, events, and methods of that category.

The WebControl class itself and other server controls that do not have visualization are derived from the System.Web.UI.Control class. For example, a PlaceHolder control or an XML control.

ASP.Net server control inherits all the properties, events, and methods of the WebControl and System.Web.UI.Control classes.

The following table shows the properties that are common to all server controls:

Property Describe
AccessKey Press the button and the Alt key at the same time to move the focus to the control.
Attributes It is a collection of arbitrary properties that do not correspond to control properties (view rendering only).
BackColor Background color.
BindingContainer Control that contains data binding.
BorderColor Border color.
BorderStyle Border style.
BorderWidth The width of the border.
CausesValidation Displays when validation is caused.
ChildControlCreated Indicates whether the child control of the server control is established.
ClientID The control ID of the HTML tag.
Context The HttpContext object associated with the server control.
Controls A collection of all controls within a control.
ControlStyle The style of the Web server control.
CssClass CSS class.
DataItemContainer If the namer performs IDataItemContainer, it provides a reference for the namer.
DataKeysContainer If the namer performs IDataKeysControl, it provides a reference for the namer.
DesignMode Indicates whether the control is used in the design interface.
DisabledCssClass When the control is disabled, get or set the CSS class to apply the rendered HTML element.
Enabled Indicates whether the control is disabled.
EnableTheming Indicates whether the topic applies to the control.
EnableViewState Indicates whether to maintain the view state of the control.
Events Gets a list of event handlers that represent controls.
Font Font settings.
Forecolor The color of the foreground.
HasAttributes Indicates whether the control has a property group.
HasChildViewState Indicates whether the child control of the current server control has any saved view state settings.
Height The pixel or percentage of the height.
Id The identifier of the control.
IsChildControlStateCleared Indicates whether the control contained inside the control has a control state.
IsEnabled Gets a value that indicates whether the control is enabled.
IsTrackingViewState Indicates whether the server control saves changes to its view state.
IsViewStateEnabled Indicates whether the view state is enabled for the control.
LoadViewStateById Indicates whether the control is loaded with an ID, not an index, to participate in loading its view state.
Page The page that contains the control.
Parent Parental controls.
RenderingCompatibility Specifies the version of the HTML that the rendered will ASP.NET compatible with.
Site The host that holds the current control when the design interface is displayed.
SkinID Gets or sets the skin that applies to the control.
Style Gets a collection of text properties that will be displayed as style properties as external labels for web server controls.
TabIndex Gets or sets an index label for a Web server control.
TagKey Gets the HtmlTextWriterTag value for the web server control.
TagName Gets the name of the control label.
TemplateControl The template that contains the control.
TemplateSourceDirectory Gets the virtual directory of the page or the control contained in the control.
ToolTip Gets or sets the text that appears when the mouse pointer stops at the Web server control.
UniqueID Unique identifier.
ViewState Gets a dictionary of state information that can save and restore the state of the server control view after multiple requests that can cross the same page.
ViewStateIgnoreCase Indicates whether the StateBag object is case insenso sensitive.
ViewStateMode Gets or sets the view state of the control.
Visible Indicates whether the server control is visible.
Width Gets or sets the width of the Web server control.

The method of the server control

The method of the server control is rendered in the following table:

Method Describe
AddAttributesToRender Add HTML properties and styles that need to render the specified HtmlTextWriterTag.
AddedControl Called after a child control is added to the control collection of control objects.
AddParsedSubObject Informs the server control that an element, XML, or HTML, has been resolved and added to the control collection of the server control.
ApplyStyleSheetSkin The style properties defined in the page style sheet are applied to the control.
ClearCachedClientID Infrastructure. Set the clientID value of the cache to null.
ClearChildControlState Removes control state information for the child control of the server control.
ClearChildState Remove view state and control state information for all server control's child controls.
ClearChildViewState Delete view state information for all server controls' child controls.
CreateChildControls Used to create child controls.
CreateControlCollection Create a new collection of controls to hold child controls.
CreateControlStyle Create a style object that implements all style-related properties.
DataBind Bind the data source to the server control and all its child controls.
DataBind(Boolean) Bind the data source and the options that can raise a DataBinding event to the server control and all its child controls.
DataBindChildren A child control that binds a data source to a server control.
Dispose Enable a server control to perform the final cleanup before it is released from memory.
EnsureChildControls Determines whether the server control contains child controls. If not, create a child control.
EnsureID Create an identifier for a control that does not have an identifier.
Equals(Object) Determines whether the specified object is equal to the current object.
Finalize Allows an object to attempt to free resources and perform additional cleanup operations before the object is recycled by the recycle bin.
FindControl(String) Search for server controls in the currently named container that have specified id parameters.
FindControl(String, Int32) Search for server controls in the currently named container that have specified id parameters and integers.
Focus Set the input focus for the control.
GetDesignModeState Gets design-time data for the control.
GetType Gets the type of the current instance.
GetUniqueIDRelativeTo Returns the pre-pinned portion of the unique ID property of the specified control.
HasControls Determines whether the server control contains child controls.
HasEvents Indicates whether an event is registered by a control or other child control.
IsLiteralContent Determines whether the server control contains only text.
LoadControlState Restore control state information.
LoadViewState Restore view state information.
MapPathSecure Retrieves the physical path to which the absolute or relative virtual path is mapped.
MemberwiseClone Create a shallow copy of the current object.
MergeStyle Copy any non-blank elements of a Web control that specifies a style, but do not overwrite any existing style elements of the control.
OnBubbleEvent Determines whether the events for the server control pass through the UI server control level of the page.
OnDataBinding Raises a data binding event.
OnInit Raises an Init event.
OnLoad Raises a load event.
OnPreRender Raises a PreRender event.
OnUnload An uninstall event is raised.
OpenFile Gets the stream used to read the file.
RemovedControl Called after the child control is removed from the control collection of the control object.
Render Displays the control to the specified HTML author.
RenderBeginTag Displays the HTML opening label of the control to the specified author.
RenderChildren Outputs the content of the server control children into the provided HtmlTextWriter object to write the content rendered on the client.
RenderContents Displays the contents of the control to the specified author.
RenderControl(HtmlTextWriter) Output server control content to the provided HtmlTextWriter object and save tracking information about the control with tracking enabled.
RenderEndTag Displays the CONTROL's HTML end label to the specified author.
ResolveAdapter Gets the control adapter that is responsible for rendering the specified control.
SaveControlState Saves a change in the state of the server control that occurs after the page is posted back to the server.
SaveViewState Save any state modified after calling the TrackViewState method.
SetDesignModeState Set up design-time data for the control.
Tostring Returns a string that represents the current object.
TrackViewState The throw control tracks changes in its view state so that it can be stored in the object's view state properties.

Let's take a look at a specific server control - a tree view control. T he tree view control belongs to the navigation control. Other navigation controls are: menu controls and SiteMapPath controls.

Add a tree view control to the page. S elect the edit node from the task... Use the tree view node editor to edit each node, as follows:

ASP.NET server controls

When the node is successfully created, the following is displayed in the design view:

ASP.NET server controls

AutoFormat... Tasks allow you to specify the format of the tree view, as follows:

ASP.NET server controls

Add a label control and a text box control to the page and name it lblmessage and txtmessage, respectively.

Write a few lines of code to make sure that when a particular node is selected, the label control displays the node text and the text box displays all the sub-nodes below it, if any. The code for the background file should look like this:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

namespace eventdemo {
   public partial class treeviewdemo : System.Web.UI.Page {

      protected void Page_Load(object sender, EventArgs e) { 
         txtmessage.Text = " "; 
      }

      protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) {

         txtmessage.Text = " "; 
         lblmessage.Text = "Selected node changed to: " + TreeView1.SelectedNode.Text;
         TreeNodeCollection childnodes = TreeView1.SelectedNode.ChildNodes;

         if(childnodes != null) {
            txtmessage.Text = " ";

            foreach (TreeNode t in childnodes) {
               txtmessage.Text += t.Value;
            }
         }
      }
   }
}

By executing the page to see the effect, you will be able to expand and collapse nodes.

ASP.NET server controls