Saturday, June 25, 2011

Thursday, June 23, 2011

Basic about " MVC "

MVC (Model-View-Controller).
If you are looking to build your web applications using a MVC approach, I think you'll find this new ASP.NET MVC Framework option very clean and easy to use. It will enable you to easily maintain separation of concerns in your applications.

http://wiki.asp.net/page.aspx/286/aspnet-mvc-framework/

Regards:GauravMCA

Thursday, June 16, 2011

Propertiies in C# 4.0

Properties provide the opportunity to protect a field in a class by reading and writing to it through the property.Another benefit of properties over fields is that you can change their internal implementation over time.

We know that data encapsulation and hiding are the two fundamental characteristics of any object oriented programming language.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.

Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.

An example, which uses a set of set/get methods, is shown below.

//SET/GET methods
//Author: mca2009.gaurav@yahoo.com
using System;
class MyClass
{
private int x;
public void SetX(int i)
{
x = i;
}
public int GetX()
{
return x;
}
}
class MyClient
{
public static void Main()
{
MyClass mc =
new MyClass();
mc.SetX(10);
int xVal = mc.GetX();
Console.WriteLine(xVal);
//Displays 10
}
}

But C# provides a built in mechanism called properties to do the above. In C#, properties are defined using the property declaration syntax. The general form of declaring a property is as follows.


{
get
{
}
set
{
}
}

Where can be private, public, protected or internal. The can be any valid C# type. Note that the first part of the syntax looks quite similar to a field declaration and second part consists of a get accessor and a set accessor.

For example the above program can be modifies with a property X as follows.

class MyClass
{
private int x;
public int X
{
get
{
return x;
}
set
{
x =
value;
}
}
}

The object of the class MyClass can access the property X as follows.

MyClass mc = new MyClass();

mc.X = 10; // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.
This is used for setting value for the data member x.
Console.WriteLine(mc.X);// displays 10. Calls the get accessor of the property X.

The complete program is shown below.

//C#: Property
//Author: mca2009.gaurav@yahoo.com
using System;
class MyClass
{
private int x;
public int X
{
get
{
return x;
}
set
{
x =
value;
}
}
}
class MyClient
{
public static void Main()
{
MyClass mc =
new MyClass();
mc.X = 10;
int xVal = mc.X;
Console.WriteLine(xVal);
//Displays 10
}
}

Remember that a property should have at least one accessor, either set or get. The set accessor has a free variable available in it called value, which gets created automatically by the compiler. We can't declare any variable with the name value inside the set accessor.

We can do very complicated calculations inside the set or get accessor. Even they can throw exceptions.

Since normal data fields and properties are stored in the same memory space, in C#, it is not possible to declare a field and property with the same name.

Static Properties

C# also supports static properties, which belongs to the class rather than to the objects of the class. All the rules applicable to a static member are applicable to static properties also.

The following program shows a class with a static property.

//C# : static Property
//Author:mca2009.gaurav@yahoo.com
using System;
class MyClass
{
private static string x;
public static string X
{
get
{
return x;
}
set
{
x =
value;
}
}
}
class Client
{
public static void Main()
{
MyClass.X = "Gaurav";
string xVal = MyClass.X;
Console.WriteLine(xVal);
//Displays Gaurav
}
}

Remember that set/get accessor of static property can access only other static members of the class. Also static properties are invoking by using the class name.

Properties & Inheritance

The properties of a Base class can be inherited to a Derived class.

//C# : Property : Inheritance
//Author: mca2009.gaurav@yahoo.com
using System;
class Base
{
public int X
{
get
{
Console.Write("Base GET");
return 10;
}
set
{
Console.Write("Base SET");
}
}
}
class Derived : Base
{
}
class MyClient
{
public static void Main()
{
Derived d1 =
new Derived();
d1.X = 10;
Console.WriteLine(d1.X);
//Displays 'Base SET Base GET 10'
}
}

The above program is very straightforward. The inheritance of properties is just like inheritance any other member.

Properties & Polymorphism

A Base class property can be polymorphicaly overridden in a Derived class. But remember that the modifiers like virtual, override etc are using at property level, not at accessor level.

//C# : Property : Polymorphism
//Author: mca2009.gaurav@yahoo.com
using System;
class Base
{
public virtual int X
{
get
{
Console.Write("Base GET");
return 10;
}
set
{
Console.Write("Base SET");
}
}
}
class Derived : Base
{
public override int X
{
get
{
Console.Write("Derived GET");
return 10;
}
set
{
Console.Write("Derived SET");
}
}
}
class MyClient
{
public static void Main()
{
Base b1 =
new Derived();
b1.X = 10;
Console.WriteLine(b1.X);
//Displays 'Derived SET Derived GET 10'
}
}

Abstract Properties


A property inside a class can be declared as abstract by using the keyword abstract. Remember that an abstract property in a class carries no code at all. The get/set accessors are simply represented with a semicolon. In the derived class we must implement both set and get assessors.

If the abstract class contains only set accessor, we can implement only set in the derived class.

The following program shows an abstract property in action.

//C# : Property : Abstract
//Author: mca2009.gaurav@yahoo.com
using System;
abstract class Abstract
{
public abstract int X
{
get;
set;
}
}
class Concrete : Abstract
{
public override int X
{
get
{
Console.Write(" GET");
return 10;
}
set
{
Console.Write(" SET");
}
}
}
class MyClient
{
public static void Main()
{
Concrete c1 =
new Concrete();
c1.X = 10;
Console.WriteLine(c1.X);
//Displays 'SET GET 10'
}
}

The properties are an important features added in language level inside C#. They are very useful in GUI programming. Remember that the compiler actually generates the appropriate getter and setter methods when it parses the C# property syntax.

This lesson teaches C# Properties in 4.0

This Link Will Help You to understand in deeply how Properties work....


@GAURAV_TARNTARAN

This article describes some features and architecture of IIS 7.0 and deployment of ASP.NET sites on IIS

This Link will Help You to see whats happning inside IIS7.0 although 7.5 is in the market but base is this so must read this link below
http://www.codeproject.com/KB/aspnet/IIS7ASPNet.aspx

@Gaurav_TarnTaran

Tuesday, June 7, 2011

Asp.net Page Life Cycle

The life cycle may be broken down into Stages and Events. The stages reflect the broad spectrum of tasks performed. The following stages take place

1) Page Request – This is the first stage, before the page life cycle starts. Whenever a page is requested, ASP.NET detects whether the page is to be requested, parsed and compiledor whether the page can be cached from the system.

2) Start – In this stage, properties such as Request and Response are set. Its also determined at this stage whether the request is a new request or old, and thus it sets theIsPostBack property in the Start stage of the page life cycle.

3) Page Initialization – Each control of the page is assigned a unique identification ID. If there are themes, they are applied. Note that during the Page Initialization stage, neither postback data is loaded, nor any viewstate data is retrieved.

4) Load – If current request is a postback, then control values are retrieved from their viewstate.

5) Validation – The validate method of the validation controls is invoked. This sets the IsValidproperty of the validation control.

6) PostBack Event Handling – Event handlers are invoked, in case the request is a postback.

7) Rendering – Viewstate for the page is saved. Then render method for each control is called. A textwriter writes the output of the rendering stage to the output stream of the page’s Response property.

8) Unload – This is the last stage in the page life cycle stages. It is invoked when the page is completely rendered. Page properties like Respone and Request are unloaded.

-------------------------------------------------------------------------------------------------------------------------

Note that each stage has its own events within it. These events may be used by developers to handle their code. Listed below are page events that are used more frequently.

PreInit – Checks the IsPostBack property. To create or recreate dynamic controls. To set master pages dynamically. Gets and Sets profile propety values.

Init – Raised after all controls are initialized, and skin properties are set.

InitComplete – This event may be used, when we need to be sure that all initialization tasks are complete.

PreLoad – If processing on a control or a page is required before the Load event.

Load – invokes the OnLoad event on the page. The same is done for each child control on the page. May set properties of controls, create database connections.

Control Events – These are the control specific events, such as button clicks, listbox item selects etc.

LoadComplete – To execute tasks that require that the complete page has been loaded.

PreRender – Some methods are called before the PreRenderEvent takes place, likeEnsureChildControls, data bound controls that have a dataSourceId set also call theDataBind method.

Each control of the page has a PreRender event. Developers may use the prerender event to make final changes to the controls before it is rendered to the page.

SaveStateComplete – ViewState is saved before this event occurs. However, if any changes to the viewstate of a control is made, then this is the event to be used. It cannot be used to make changes to other properties of a control.

Render – This is a stage, not an event. The page object invokes this stage on each control of the page. This actually means that the ASP.NET server control’s HTML markup is sent to the browser.

Unload – This event occurs for each control. It takes care of cleanup activities like wiping the database connectivities.