Sunday, 15 July 2012

ASP.NET User Controls


User Controls are customized controls that can be created based on the needs of users. ASP.NET  provides this feature to assist developers to create controls with special or customized  functionalities that standard Web Server Control does not have.  User Controls are usually used in combination of a set of Web Server controls and functionality for reuse in multiple places of a site. User Controls look like any Web Forms with UI files to design the controls layout as well as the code behind file to write the code. Although they look similar, there are some noticeable difference when compared with a common Web Form.


i.      The file extension of a User Control is .ascx.
ii.     Contains @ Control directive in the UI File that defines the control’s configuration.
iii.    Only works when hosted in a Web Form. It is not executable as an independent file.
iv.    , and
elements are not used in UI file.

User controls are hosted in a Web Form by registering it in the host file. Below is an example how the User Control is registered.


<%@ Register src="UserControls/UC1.ascx" tagname="UC1" tagprefix="uc1" %>


Src”  refers to the User Control file. “Tagname” is used to identify a User Control file. "Tagprefix” is used to specify unique namespace for the User Control. In Visual Studio, the @ Register directive will be automatically populated in the host file when the User Control is embedded in the host file.

A User Control may have custom build properties which can be consumed by the host file. These properties are created  in the same way as how properties are created in a Web Form.

A User Control may also have Web Server Controls such as Buttons that would require an Event Handler such as Button_Click to be created to respond to user’s action. However, events raised by Web Server Controls in User Controls would not be available in User Control files. To resolve this problem, an event needs to be defined and raised in the User Control file and notified to the host file.

Tuesday, 12 June 2012

ASP.NET Validators


Validation controls are added on the page to enable user input validation. Validation Controls work by referencing input control such as Textboxes, RadioButtons, DropDownList etc. to a validator. When a user enters all the details in a form and clicks on the submit button, the validators associated with each input control responds by validating values that were entered. If the input validation fails, the page will be returned.


Validators can also be enabled or disabled by setting them in the properties. These validators can be grouped together to enable or disable them in groups. 

There are a few types of validation controls that can be applied on the web.



RequiredFieldValidator
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
                               runat="server"
                               ErrorMessage="RequiredFieldValidator">
<\asp:RequiredFieldValidator>


Simple and  very useful validator that is used to validate and ensure input controls are entered with some value and prevents any attempt to leave the controls empty.



Example
<asp:RequiredFieldValidator ID="RequiredValidationCity"
                               runat="server" 
                               ErrorMessage="Please enter a City"
                               ForeColor="Red"
                               ControlToValidate="txtState" 
                               SetFocusOnError="True">&
lt\asp:RequiredFieldValidator>








CompareValidator

<asp:CompareValidator ID="CompareValidator1" runat="server" 

                       ErrorMessage="CompareValidator">asp:CompareValidator>





Compares the value between 2 input controls or 1 input control and 1 fixed value.



Example
<asp:CompareValidator ID="CompareValidatorBirthDay1"

                      runat="server"

                      ControlToValidate="txtFrom"

                      ErrorMessage="Invalid Date Range"

                      ForeColor="Red"

                      Operator="LessThanEqual"

                      SetFocusOnError="True"

                      ValueToCompare="01/01/1900"

                      ControlToCompare="txtTo"

                      Type="Date">asp:CompareValidator>