9 February 2014

Server Side Controls in Asp.net

     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.

The below Namespaces used by server controls in asp.net

·         System.Web.UI.HtmlControls.HtmlControl
·         System.Web.UI.WebControls.WebControl

We have different server side controls
·         HTML Server Controls
·         Web Server Controls
·         Validation Controls
·         List Controls
·         Rich Controls
·         User Controls
·         Custom Controls


HTML Server Controls:

      The HTML server controls are Hypertext Markup Language (HTML) elements that include a runat=server attribute. The HTML server controls have the same HTML output and the same properties as their corresponding HTML tags. In addition, HTML server controls provide automatic state management and server-side events. HTML server controls offer the following advantages:

·         The HTML server controls map one to one with their corresponding HTML tags.
·         When the ASP.NET application is compiled, the HTML server controls with the runat=server attribute are compiled into the assembly.
·         Most controls include an OnServerEvent for the most commonly used event for the control. For example, the <input type=button> control has an OnServerClick event.
·         The HTML tags that are not implemented as specific HTML server controls can still be used on the server side; however, they are added to the assembly as HtmlGenericControl.
·         When the ASP.NET page is reposted, the HTML server controls keep their values.

The System.Web.UI.HtmlControls.HtmlControl base class contains all of the common properties. HTML server controls derive from this class. 

To use an HTML server control, use the following syntax (which uses the HtmlInputText control as an example):
<input type="text" value="hello world" runat=server />

These are the following HTML server controls 
·         HtmlAnchor
·         HtmlButton
·         HtmlForm
·         HtmlImage
·         HtmlInputButton
·         HtmlInputCheckBox
·         HtmlInputFile
·         HtmlInputHidden
·         HtmlInputImage
·         HtmlInputRadioButton
·         HtmlInputText
·         HtmlSelect
·         HtmlTable
·         HtmlTableCell
·         HtmlTableCell
·         HtmlTextArea

Web Server Controls:

        Web controls are very similar to the HTML server controls such as ButtonTextBox, and Hyperlink, except that Web controls have a standardized set of property names. Web server controls offer the following advantages:

·         Make it easier for manufacturers and developers to build tools or applications that automatically generate the user interface.
·         Simplify the process of creating interactive Web forms, which requires less knowledge of how HTML controls work and make the task of using them less prone to errors.

The System.Web.UI.WebControls.WebControl base class contains all of the common properties. Most of the Web server controls derive from this class.

To use a Web server control, use the following syntax (which uses the TextBox control as an example):

<asp:textbox text="hello world" runat=server />

Basic Web Controls

       Basic Web controls provide the same functionality as their HTML server control counterparts. However, basic Web control include additional methods, events, and properties against which you can program
   
 1) Buttons
Validation Controls:

        Validation controls are used to validate the values that are entered into other controls of the page. Validation controls perform client-side validation, server-side validation, or both, depending on the capabilities of the browser in which the page is displayed.
        
        The validation control classes inherit from the Base Validator class and inherit its properties and methods. Therefore, it would help to take a look at the properties and the methods of this base class, which are common for all the validation controls: 
           
        Validation controls offer the following advantages:
·         You can associate one or more validation controls with each control that you want to validate.
·         The validation is performed when the page form is submitted.
·         You can specify programmatically whether validation should occur, which is useful if you want to provide a cancel button so that the user can exit without having to fill valid data in all of the fields.
·         The validation controls automatically detect whether validation should be performed on the client side or the server side.

The Common Properties for all validation controls are 
      1) Control to Validate : the control to be validated 
      2) Display: How the message has to displayed to user 
      3) Enable Client Script: Whether to validate client side 
      4) Enabled : whether validate control is enabled or disabled
      5) Error Message: Message to be displayed to the user on error
      6) Is Valid : Indicates the value of the control is valid or not 
      7)  Set Focus on Error:Indicates whether in case of invalid control,the focus should be switched to the related control
      8)  Validation group: It is logical group of multiple validations,where this control belongs         
      9)   Validate():It will revalidate the control and updates IsValid Property
   
Note A client-side validation catches errors before a post back operation is complete. Therefore, if you have combinations of client-side and server-side validation controls on a single page, the server-side validation will be preempted if a client-side validation fails.

For more information about individual validation controls that are available in ASP.NET, refer to the following Microsoft Web sites:
List Controls

             List controls are special Web server controls that support binding to collections. You can use list controls to display rows of data in a customized, templated format. All list controls expose DataSource and DataMember properties, which are used to bind to collections.

             List controls can bind only to collections that support the IEnumerable, ICollection, or IListSource interfaces. For example, a Microsoft Visual C# .NET sample page appears as follows:

<% @page Language="C#" %>
<script runat="server">
public void page_load()
{
       String[] mystringarray =new string[] { "One","two","three"};
       rptr.DataSource=mystringarray 
       rptr.DataBind();

}
</script>
<html>
 <body>
<asp:repeater id=rptr runat="server">
     <itemtemplate><%# container.DataItem%><br></ItemTemplate>
</asp:repaeater>
</body>
</html>
                                        
A Microsoft Visual Basic .NET sample page appears as follows:

<% @page Language="vb" %>

<script runat="server">
public sub page_load()
       Dim mystringarray as string()
       mystringarray  = new String() {"one","two","three"}
       rptr.DataSource=mystringarray 
       rptr.DataBind()
End Sub
</script>
<html>
 <body>
<asp:repeater id=rptr runat="server">
     <itemtemplate><%# container.DataItem%><br></ItemTemplate>
</asp:repaeater>
</body>
</html>

The output appears as follows:
one 
two 
three

For more information about individual list controls that are available in ASP.NET
List Box
CheckBoxList
RadioButtonList
Repeater
DataList
DataGrid
DropDownList

Rich Controls

In addition to the preceding controls, the ASP.NET page framework provides a few, task-specific controls called rich controls. Rich controls are built with multiple HTML elements and contain rich functionality. Examples of rich controls are the Calendar control and the Ad Rotator control.

For more information about individual rich controls that are available in ASP.NET, refer to the following Microsoft Web sites:

Ad Rotator
Calendar
Xml

No comments:

Post a Comment