Wednesday, January 27, 2010

What is difference between Load event and PreRender event?

Load event happen before any control events and PreRender event happens after any control event.

What is page execution life cycle? OR Life – Cycle Events?

Following are the sequence of events that raised whenever you request a page:
     1)    PreInit
     2)    Init
     3)    InitComplete
    4)    PreLoad
   5)    Load
  6)    LoadComplete
  7)    PreRender
  8)    PreRenderComplete
  9)    SaveStateComplete
 10)    Unload
Note: - View State is not loaded until after the InitComplete event. Data posted to the server from a form control, such as a TextBox control is also not available until after this event.
 1)    PreInit: - Use this event for the following:
        •    Check the IsPostBack property to determine whether this is the first time the page is being processed.
        •    Create or re-create dynamic controls.
        •    Set a master page dynamically.
        •    Set the Theme property dynamically.
        •    Read or set profile property values.
Note: - If the request is a postback, the values of the controls have not yet been restored form view state. If you set a control property at this stage, its value might be overwritten in the next event.
  2)    Init: - Raised after all controls have been initialized and any skin settings have been applied. Use this event to read or initialize control properties.
  3)    InitComplete: - Raised by the Page object. Use this event for processing tasks that require all initialization be complete.
  4)    PreLoad: - Use this event if you need to perform processing on your page or control before the Load event. Before the Page instance raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.
  5)    Load: - The Page calls the OnLoad event methods on the Page, then recursively does the same for each child control, which does the same for each of its child controls until the page and all the controls are loaded. Use the OnLoad event method to set properties in controls and establish database connections.
  6)    LoadComplete: - Use this event for tasks that require that all other controls on the page be loaded.
  7)    PreRender: - Before this event occurs:
    •    The Page object calls EnsureChildControls for each control and for the page.
    •    Each data bound control whose DataSourceID property is set calls its DataBind method.
The PreRender event occurs for each control on the page. Use the event to make final changes to the contents of the page or its controls.
  8)    PreRenderComplete: - Occurs before the page content is rendered.
The PreRenderComplete event is raised when the pre-render stage of the page life cycle is complete. At this stage of the page life cycle, all controls are created, any pagination required is completed, and the page is ready to render to the output.
This is the last event raised before the page’s view state is saved.
   9)    SaveStateComplete: - Before this event occurs, ViewState has been saved for the page and for all controls. Any changes to the page or controls at this point will be ignored.
Use this event perform tasks that require view state to be saved, but that do not make any changes to controls.
   10)    Unload: - This event occurs for each control and then for the page. In controls, use this event to do final cleanup for specific controls, such as closing control-specific database connections.
For the page itself, use this event to do final cleanup work, such as closing open files and database connections, or finishing up logging or other request-specific tasks.

Tuesday, January 26, 2010

Page Life – cycle Stages.

Following are the page stages: -
1) Page request
2) Start
3) Page initialization
4) Load
5) Validation
6) Postback event handling
7) Rendering
8) Unload
1) Page request: - The page request occurs before life cycle begins. When the page is requested by a user, ASP.NET determines whether the page needs to be parsed and compiled (therefore beginning the life of a page), or whether a cached version of the page can be sent in response without running the page.
2) Start: - In the start step, page properties such as Request and Response are set. At this stage, the page also determines whether the request is a postback or a new request and sets the IsPostBack property. Additionally, during the start step, the page’s UICulture property is set.
3) Page initialization: - During page initialization, controls on the page are available and each control's UniqueID property is set. Any themes are also applied to the page. If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.
4) Load: - During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.
5) Validation: - During validation, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page.
6) Postback event handling: - If the request is a postback, any event handles are called.
7) Rendering: - Before rendering, view state is saved for the page and all controls. During the rendering phase, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream ot the page’s Response property.
8) Unload: - Unload is called after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and any cleanup is performed.

How many validation controls do you know?


With ASP.NET, there are six (6) controls included. They are:
1). RequiredFieldValidator
2). RangeValidator
3). CompareValidator
4). RegularExpressionValidator
5). CustomValidator
6). ValidationSummary
Note: - All of the validation controls inherit from the base class BaseValidator so they all have a series of properties and methods that are common to all validation controls. They are:
ControlToValidate – This value is which controls the validator is applied to.
ErrorMessage – This is the error message that will be displayed in the validation summary.
IsValid – Boolean value for whether or not the control is valid.
Validate – Method to validate the input control and update the IsValid property.
Display – This controls how the error message is show. Here are the possible options:
·         None (The validation message is never displayed.)
·         Static (Space for the validation message is allocated in the page layout.)
·         Dynamic (Space for the validation message is dynamically added to the page if validation fails.)
1). RequiredFieldValidator Control
Enables you to require a user to enter a value in a form field.
Example: -
RequiredFieldValidation: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
            ControlToValidate="TextBox1" Display="Dynamic"
            ErrorMessage="* You must enter a value into textbox1"></asp:RequiredFieldValidator>
2). RangeValidator Control: -
Enables you to check whether a value falls between a certain minimum and maximum value. The attributes that are necessary to this control are: MaximumValue, MinimumValue, and Type.
Example: -
Enter a data from 1981:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server"
            ControlToValidate="TextBox1"
            ErrorMessage="* The date must be 12/15/1981 and 1/1/2010"
            MaximumValue="12/15/1981" MinimumValue="1/1/2010" Type="Date"></asp:RangeValidator>
3). CompareValidator Control: -
Enables you to compare a value against another value or perform a data type check.

Example: -
TextBox1: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
TextBox2: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
            ErrorMessage="* You must enter the same values into both text box."
            ControlToCompare="TextBox2" ControlToValidate="TextBox1" Display="Dynamic"></asp:CompareValidator>
Note: - The data type can be one of: Currency, Double, Date, Integer or String. String being the default data type.
4). RegularExpressionValidator Control:
Enables you to compare a value against a regular expression.
Example: -
E-mail: - <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
            ControlToValidate="TextBox1" Display="Dynamic"
            ErrorMessage="* Please enter a valid e-mail address."
            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
5). CustomValidator Control: -
Enables you to perform custom validation. Here we get to write out own functions and pass the control value to this function.
Example: -
Field:- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
            ClientValidationFunction="ClientValidate" OnServerValidate="ServerValidate" ControlToValidate="TextBox1"
            ErrorMessage="* This box is not valid." Display="Dynamic"></asp:CustomValidator>
Note: - There are two new attributes ClientValidationFunction and OnServerValidate. ClientValidationFunction is usually a javascript function  included in the html to the user. OnServerValidate is the function that is server-side to check for validation if client does not support client-side validation.
Client Validation functions:
<script language="javascript">

/* Code goes here */

</script>
Server Validation functions:
public void ServerValidate(object obj, ServerValidateEventArgs args)
    {
        /*Code goes here */
    }
6). ValidationSummary Control: -
Enables you to display a summary of all validation error in a page.
Example: -
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
            DisplayMode="List" HeaderText="Errors:" ShowMessageBox="True" />
Note: - The validation summary control will collect all the error message of all the non – valid controls and put them in a tidy list. The list can be either show on the web page or with a popup box (by specifying ShowMessageBox = “True”)

Monday, January 25, 2010

What is the difference between login controls and Forms authentication?

  • Forms authentication can be easily implemented using login controls without writing any code.
  • Login control performs functions like prompting for user credentials, validating them and issuing authentication just as the FormsAuthentication class.
  • However, all that’s needs to be dne is to drag and drop the use control from the tool box to have these checks performed implicitly.
  • The FormsAuthentication class is used in the background for the authentication ticket and ASP.NET membership is used to validate the user credentials.