Archive for November, 2008
ASP.NET Dynamic Compilation
This post explains about ASP.NET Dynamic Compilation and code-behind pages in more details. When you create a ASP.NET page in web site it actually creates a .NET code behind class. The entire contents of the page is compiled into a .NET class.
When you sent a request for ASP.NET page, Framework checks for the corresponding class to the page and if the class not exists, Framework compiles the page into a new .NET class and stores the compiled class in the temporary ASP.NET Folder located at
\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files.
In future if request comes to the same page, the page is not compiled again.The previously compiled class is executed and returns to the browser. This process is called ASP.NET Dynamic Compilation.
If the ASP.NET Page is modified corresponding .NET Class is automatically deleted and when new request comes for that page it compiles the modified page into a new .NET Class.
You can even pre-compile your ASP.NET application using aspnet_compiler.exe command line tool. when you pre-compile your application users doesn’t experience the compilation delay for the first request.
- You can disable dynamic compilation for a single , pages in a folder or for entire web site. Use the CompilationMode attribute in <%page%> directive which allows you to disable the dynamic compilation for a single page.
- You can disable dynamic compilation for entire application by specifying CompilationMode attribute in Pages section of the Web.config file.
Disabling the Dynamic Compilation is useful when you have thousands of pages in web site.
Note: You can not disable the dynamic compilation for pages that include server-side code and pages that contain data binding expressions.
Code-Behind Pages
ASP.NET Framework allows you create two types of pages. one where you can write page code and declare page controls in a single page. the other way where you can declare UI controls in one page and page code in separate page normally we call this as a code-behind page.
code-behind page work in a different way in ASP.NET 2.0 compared to ASP.NET 1.1. In version 1.1 pages are related by inheritance and in version 2.0 pages are related by inheritance and partial classes.
The problem with the version 1.1 method is any control that you declared in ASP.NET Page needs to be declared in the code-behind file and with exactly same control-ID.
In Version 2.0 the association of ASP.NET Page and Code-Behind page no longer related by inheritance but through a new concept partial classes. It enables you to declare a class in more than one physical-file. Any members of one partial class is accessible to the any other partial class of the same class.
The advantage of using partial classes is declaring a control in ASP.NET Page is available in code-behind page and anything declared in code-behind file will automatically available in Page.
Related Posts:
2 commentsUsing the ASP.NET Panel Control
Introduction
This post speaks about using Panel Control in ASP.NET Pages. Panel Control allows you to work with a group of ASP.NET Controls. You can use it to show or hide a group of controls.
Example:
The Panel control supports the following properties
- DefaultButton When you set a value to this property, the default button in the panel is invoked when you press the Enter Button.
- Direction Enables you to set or get the direction of the text. Possible values are NotSet, LeftToRight and RightToLeft.
- GroupingText Enables you to render the panel control as a fieldset with legend.
- ScrollBars Enables you to display the scrollbars around the panel control.
By default panel renders with the Div tag and when you set the GroupingText property panel renders the fieldset tag.
Related Posts:
No commentsView State vs Control State
Control State is another state management technique which is essentially a private view state for your control.
In order to preserve the page and control data over round trips to the web server, traditionally we are using View State. View State Property maintains information in hidden field and the data is encrypted.
If you disable the View State in your page, the custom controls which used in your page has a chance of losing their state.
To Address the above issue ASP.NET 2.0 has introduced Control State which is private View State for the control and preserves the state of the control even when you turn off the View State.
Use the Control State property when you are developing the Custom Control by overriding the SaveControlState method to save the data in Control State.
Note that Control State Can not be disabled.
Related Posts:
No commentsC# IDE Tips & Tricks Part 2
This post explains the some more tips on using Visual C# IDE to enhance developer productivity.
- Solution Configurater Right click your solution in your IDE then select the ConfigurationManager you see the following window
where you have the option to select the project for building. We can select the required project in the solution and can you can build the project.
- Solution Wide Comments You can add task lists and comments to your solution. To view the task list for your solution select view menu and click on TaskList option. Ctrl + W, T you will see the following window
You can add the task list items to window by writing the following snippet in the code file.
Related Posts:
No commentsSingleton Design Pattern in C#
Best known Creational Design Pattern is Singleton. We can implement this pattern in different ways. I am explaining some of the ways where we can implement in C#.
We will have the following concepts in implementing the Singleton pattern
- We declare a private constructor which prevents other classes to create an instantiation of the singleton class. It also prevents the sub classing.
- We declare a class as sealed so that it can not be instantiated.
- We declare a static variable for holding the reference of a singleton class.
- We declare a public property to return the singleton instance.
- We declare a method for returning the message from the singleton class.
The problem with the above code is it is not a thread –safe code. For example if two threads evaluates the if condition in the above code and both returns the true.
We will rewrite the code for thread safety
The above implementation is thread safe but having the performance issue.
Every time the lock is acquired whenever you request the object.
We can write the above code with out implementing the locks as follows
The static constructor in C# only be called whenever you create a instance for the class.
Related Posts:
No commentsC# IDE Tips & Tricks Part 1
C# Developers have been spending most their day activities
with Visual studio IDE. They may have to understand the following activities to do their job.
- Understanding Code Developer must be able analyze the relationship between classes and what API it is using for implementing the logic.
- Navigating Code Developer may or may not know where he want to navigate to the code and keep track of visited files in the code.
- Writing & Modifying Code Developers may require to modify the existing code like refractoring which involves make the code readable and fix bugs etc
- Debugging & Testing Understanding the using of debugger tools and writing unit test cases.
Visual studio provides the tools for all the activities mentioned above to make the developer efficient and productive. This post speaks about using the tools available in VS 2008 sp1. It also discusses about the tool coderush express which Microsoft partnering and allows the developers to download and use freely.
1. One of the short cut for goto definition is F12
For example you have an identifier and you want to know where it is declared just press F12. If the identifier is not your project then it shows the metadata of the identifier. eg we are viewing string class
Related Posts:
No commentsC# ?? operator
The ?? operator in C# called null-coalescing operator is used to define a default value for a nullable value types and reference types. It returns the left-hand operand if it is not null and returns right-hand operand if it is null.
example
// ?? operator example
. int? x = null;
int y = x ?? –1;
// Here the value of y will be –1.
int i = GetValue() ?? default(int);
Assigns the return value of the method to i if return value is not null other wise it assigns default value
It can be used with reference type as follows
string s = GetStringValue(); // ?? also works with reference types. // Display contents of s, unless s is null, // in which case display "Unspecified". Console.WriteLine(s ?? "Unspecified");
Related Posts:
No commentsUsing keyword in C#
Introduction
Using keyword in C# can be used as directive and as well as statement.
When you use using as Directive you will get the following advantages
- You can use the types in a namespace by declaring with using keyword.
example: using Systetm.Web;
- You can define a alias for the nested namespaces
example: using alias = CompanyName.ProjectName;
When you use using as Statement it defines the scope and allows the programmer to release the resources used by the object in the defined scope.
The object mentioned in the using statement must implement IDisposable interface. A using statement is can be exited when the end of the using statement is reached.
example :
Font font2 = new Font("Arial", 10.0f);
using (font2)
{
// use font2
}
Multiple objects can be used in with a using statement, but they must
be declared inside the using statement.
example:
using (ResourceType r1 = e1, r2 = e2, …, rN = eN) statement
it is equivalent to the following code
using (ResourceType r1 = e1)
using (ResourceType r2 = e2)…
using (ResourceType rN = eN)
statement
Related Posts:
No commentsWhat is Windows Azure?
Introduction
Windows Azure is an operating system for windows cloud. This post speaks about key features and how can we use windows Azure.
Related Posts:
No commentsNew .NET Logo
Microsoft officially announced new logo for Visual studio suite at PDC 2008.
The logo is on light background and dark background.
Why this logo?
It stands for consistency, robustness and greater user experiences. It is reflected in newer brands such as silver light, surface and more. The letter ‘N’ in the logo will become shorthand for the .NET brand name.
![Recommend [kalyanms1]](http://s3.amazonaws.com/arkayne-media/img/badge/02me.png)