mobile ads

Wednesday, 30 October 2013

ASP.NET Page Validation Controls


  • RequiredFieldValidator—Enables you to require a user to enter a value in a form field.
  • RegularExpressionValidator—Enables you to compare a value against a regular expression.
  • RangeValidator—Enables you to check whether a value falls between a certain minimum and maximum value.
  • CompareValidator—Enables you to compare a value against another value or perform a data type check.
  • CustomValidator—Enables you to perform custom validation.
  • ValidationSummary—Enables you to display a summary of all validation errors in a page.
You can associate the validation controls with any form controls included in ASP.NET Framework. For example, if you want to require a user to enter a value into a TextBox control, you can associate a RequiredFieldValidator control with the TextBox control.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        Name: <asp:TextBox ID="tbName" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator 
                ID="RequiredFieldValidator1" runat="server"
                ErrorMessage="You must enter your name" 
                ControlToValidate="tbName"  ForeColor="Red"/>
        <br />
        Date of birth: <asp:TextBox ID="tbDOB" runat="server" />
        <asp:RegularExpressionValidator
                ID="RegularExpressionValidator1" runat="server"
                ErrorMessage="Enter a date in the form MM/DD/YYYY"
                ControlToValidate="tbDOB"  ForeColor="Red"
                ValidationExpression="(0[1-9]|1[012])/([1-9]|0[1-9]|[12][0-9]|3[01])/\d{4}"
        />
        <br />
        Amount of candies you eat daily (< than 1000): 
        <asp:TextBox ID="tbCandy" runat="server" />
        <asp:CompareValidator 
            ID="CompareValidator1" runat="server"
            ErrorMessage="Number is not < 1000"
            ControlToValidate="tbCandy"
            Operator="LessThan"  ForeColor="Red"
            ValueToCompare="1000" Type="Integer" />
        <br />
        Number of Doctor visits (1 - 10):
        <asp:TextBox ID="tbDr" runat="server" />
        <asp:RangeValidator 
                ID="RangeValidator1" runat="server"
                ErrorMessage="Enter a value from 1 to 10"
                ControlToValidate="tbDr"  ForeColor="Red"
                MinimumValue="1" MaximumValue="10" />
        <br />
        

        <asp:ValidationSummary runat="server" 
            ID="validationSummary"  ForeColor="Blue"/>

        <br />
        <asp:Button runat="server" Text="Submit" ID="btnSubmit" />
    </div>
    </form>
</body>
</html>


For detailed description in MSDN, Click here

Tuesday, 29 October 2013

WPF Introduction


Windows Presentation Foundation (WPF) is a next-generation presentation system for building Windows client applications with visually stunning user experiences. With WPF, you can create a wide range of both standalone and browser-hosted applications.
The core of WPF is a resolution-independent and vector-based rendering engine that is built to take advantage of modern graphics hardware. WPF extends the core with a comprehensive set of application-development features that include Extensible Application Markup Language (XAML), controls, data binding, layout, 2-D and 3-D graphics, animation, styles, templates, documents, media, text, and typography. WPF is included in the Microsoft .NET Framework, so you can build applications that incorporate other elements of the .NET Framework class library.

WPF can be used to develop the following types of applications:

1. Standalone Applications (traditional style Windows applications built as executable assemblies that are installed to and run from the client computer).
2. XAML browser applications (XBAPs) (applications composed of navigation pages that are built as executable assemblies and hosted by Web browsers such as Microsoft Internet Explorer or Mozilla Firefox).
3. Custom Control Libraries (non-executable assemblies containing reusable controls).
4. Class Libraries (non-executable assemblies that contain reusable classes).

WPF Architecture

For every new technology, it is very essential to have a clear idea about its architecture. So before beginning your application, you must grab a few concepts. If you would not like to know WPF in detail, please skip this section. As mentioned earlier, WPF is actually a set of assemblies that build up the entire framework. These assemblies can be categorized as:
  • Managed Layer
  • UnManaged Layer
  • Core API
Managed Layer: Managed layer of WPF is built using a number of assemblies. These assemblies build up the WPF framework, communicate with lower level unmanaged API to render its content. The few assemblies that comprise the WPF framework are:
  1. PresentationFramework.dll: Creates the top level elements like layout panels, controls, windows, styles, etc.
  2. PresentationCore.dll: It holds base types such as UIElement, Visual from which all shapes and controls are Derived in PresentationFramework.dll.
  3. WindowsBase.dll: They hold even more basic elements which are capable of being used outside the WPF environment like Dispatcher object, Dependency Objects. I will discuss each of them later.
Unmanaged Layer (milcore.dll): The unmanaged layer of WPF is called milcore or Media Integration Library Core. It basically translates the WPF higher level objects like layout panels, buttons, animation, etc. into textures that Direct3D expects. It is the main rendering engine of WPF.
WindowsCodecs.dll: This is another low level API which is used for imaging support in WPF applications. WindowsCodecs.dll comprises a number of codecs which encode / decode images into vector graphics that would be rendered into WPF screen.
Direct3D: It is the low level API in which the graphics of WPF is rendered.
User32: It is the primary core API which every program uses. It actually manages memory and process separation.
GDI & Device Drivers: GDI and Device Drivers are specific to the operating system which is also used from the application to access low level APIs.

For more information,Click here

Wednesday, 16 October 2013

Difference between Response.Redirect and Server.Transfer

Response.Redirect involves an extra round-trip to the server whereas Server.Transfer conserves server resources by avoiding that extra round-trip. Server.Transfer just changes the focus of the page by directly communicating with server.
         Response.Redirect
  1. Response.Redirect() will send you to a new page, update the address bar and add it to the Browser History. On your browser you can click back.
  2. It redirects the request to some plain HTML pages on our server or to some other web server.
  3. It causes additional roundtrips to the server on each request.
  4. It doesn’t preserve Query String and Form Variables from the original request.
  5. It enables to see the new redirected URL where it is redirected in the browser (and be able to bookmark it if it’s necessary).
  6. Response. Redirect simply sends a message down to the (HTTP 302) browser. 


  1. Server.Transfer
  2. Server.Transfer() does not change the address bar, we cannot hit back.One should use Server.Transfer() when he/she doesn’t want the user to see where he is going. Sometime on a "loading" type page.
  3. It transfers current page request to another .aspx page on the same server.
  4. It preserves server resources and avoids the unnecessary roundtrips to the server.
  5. It preserves Query String and Form Variables (optionally).
  6. It doesn’t show the real URL where it redirects the request in the users Web Browser.
  7. Server.Transfer happens without the browser knowing anything, the browser request a page, but the server returns the content of another.

machine.config and web.config

Machine.Config file specifies the settings that are global to a particular machine.  It is specifically used to store machine and application settings global to all asp.net web sites running in IIS in a computer. A system can have only one machine.config computer. Each .NET Framework such as 1.1, 2.0, 4.0 will have its own machine.config in the %runtime install path%\Config folder.
 This file is located at the following path:
 [Windows Directory]\Microsoft.NET\Framework\[Framework Version]\CONFIG\machine.config
 As web.config file is used to configure one asp .net web application, same way Machine.config file is used to configure the application according to a particular machine. That is, configuration done in machine.config file is affected on any application that runs on a particular machine. Usually, this file is not altered and only web.config is used which configuring applications.
 You can override settings in the Machine.Config file for all the applications in a particular Web site by placing a Web.Config file in the root directory of the Web site as follows:
\InetPub\wwwroot\Web.Config
If you want to override a setting from machine.config for a particular asp.net web site, then you can re-write in web.config of that web site. An asp.net can have any number of web.config files.

Friday, 11 October 2013

Difference between Convert.ToString() and .ToString()

We can convert the integer i using i.ToString () or Convert.ToString
The basic difference between them is the Convert function handles NULLS while i.ToString () does not; it will throw a NULL reference exception error. So as good coding practice using convert is always safe.


You can create a class and override the toString method to do anything you want.

The Convert.toString converts the specified value to its equivalent string representation.