Pages

HttpContext is Null


In ASP.Net application, HttpContext type will be instantiated for current thread and it loads all the context values related to the Request sent to the server. While we use the Asynchronous concept in the ASP.Net application, the HTTPContext will not have a reference or values in the asynchronous thread. Apart from the normal Page execution thread, new threads will be created for every Async task we are using, So while referring those Context objects Care should be taken. Otherwise the application will throw error.
HttpContext.Current object will be null, when we are using Asynchronous task [PageAsyncTask].
To avoid this or overcome this, capture the needed values from the base thread and share it with a static class to the other threads. This capturing of values should happen before the invoking of Async Calls.

Mime Content Types for office 2007 documents

The Content Type of the office 2007 files are different from the normal office documents. the files are strored and maintained internally as xml formats. While uploading these documents they are uploaded in octet stream[binary stream].
For more details check this link. openxmldeveloper.org

The Content Types of the office 2007 files is listed below.

File ExtensionContent Type
docmapplication/vnd.ms-word.document.macroEnabled.12
docxapplication/vnd.openxmlformats-officedocument.wordprocessingml.document
dotmapplication/vnd.ms-word.template.macroEnabled.12
dotxapplication/vnd.openxmlformats-officedocument.wordprocessingml.template
ppsmapplication/vnd.ms-powerpoint.slideshow.macroEnabled.12
ppsxapplication/vnd.openxmlformats-officedocument.presentationml.slideshow
pptmapplication/vnd.ms-powerpoint.presentation.macroEnabled.12
pptxapplication/vnd.openxmlformats-officedocument.presentationml.presentation
xlsbapplication/vnd.ms-excel.sheet.binary.macroEnabled.12
xlsmapplication/vnd.ms-excel.sheet.macroEnabled.12
xlsxapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet
xpsapplication/vnd.ms-xpsdocument

Manual verification of Validation Controls before getting submitted using Javascript

The Validation controls plays a major role in validation client data before it is getting posted in to the server. This is done by placing the appropriate validation controls required. This takes care of data validation before posting on its own. Suppose this same task has to handle even when validation controls are present. Let us see this.
A JavaScript function displayed below will takes care of a similar task.
function VerifyValidators()
{
//To verify the validators are passed in data validation.
//This Page_ClientValidate() function will return true if all the validation are passed else it returns false.
if (Page_ClientValidate() == true)
{
form1.submit();
}
}

The Page_ClientValidate() function will not be included in the page by default. To implement this function we need a minimum a single validation control. Then, this function is rendered as a part of any validation control by default. By using this function, we can check the Data Validation even before it gets posted to the server. Some times when using this functionality the page may not be posted to the server, then page has to be submitted to the server manually as given in the function or in any other way.

ViewState Vs ControlState

we all know, in asp.net 1.x the web controls use the Viewstate to maintain state between postbacks. this involves more data to be transfered from and to the server. the best practice is to disbale ViewState when it is not required, but still this is problem in many other cases, suppose we use the DataGrid controls, which consumes most of the ViewState normally, the ViewState cannot be disabled because it has to support other events related to that control. the DataGrid page number, event command are stored in the Viewstate, so this is more inconvinient.
In Asp.Net 2.0, the Viewstate concept is changed and they have introduced the controlstate, which is a part of Viewstate, only for the purpose to maintain control state, not the content of it. Control state is another type of hidden state reserved exclusively for controls to maintain their core behavioral functionality, whereas view state only contains state to maintain the control's contents (UI). Technically, control state is stored in the same hidden field as view state (being just another leaf node at the end of the view state hierarchy), but if you disable view state on a particular control, or on an entire page, the control state is still propagated. this enables us to use more Web Controls, as the viewstate is maintained internally. this will not support DataGrid, because it is part of Asp.Net 1.x, the new controls of Asp.Net 2.0 will support this feature. even the viewstate is disabled the event triggered, other control properties are maintained in the controls state.

The supported controls are listed below.
Control -> Properties Stored in Control State
CheckBoxList -> PreviousItemCount, PreviousOffset
ContentPager -> CurrentPage, PageCount
DetailsView -> PageIndex, Mode, DefaultMode
FormView -> PageIndex, Mode, DefaultMode
GridView -> EditIndex, SelectedIndex, PageIndex, SortDirection, SortExpression
ListControl -> SelectedIndices
(base class for BulletedList, CheckBoxList, DropDownList, ListBox, and RadioButtonList)
LoginView -> TemplateIndex
MultiView -> ActiveViewIndex
Table -> CurrentRow, ViewMode

Partial Classes

Partial Classes allow user to split the class file accross multiple source files. The benifit behind this approach is to hide the funtionality of the class. the same class can be created in may places and appropriate methods can be placed in the code file so that it derived classes can focus on significant part alone.

Interfaces

Interface are a type similar to class, where it cannot have the Method Content, rather it has the definition of Method. It will not have properties and members variables. The class which inherits the interface should implement all the methods of the interface. The interface forces the derived class to implement; otherwise it will not get compiled.

The below lists some of common interfaces of .Net
1. IComparable - Implemented by types whose value can be ordered, it is used for sorting.
2. Idisposable - Defines this method to dispose of an object manually, this is important most commonly used Interface when dealing with more big objects, and also to release the object which holds the resource like database.
3. IConvertible - Enables the class to base type such as string, int or bool.
4. IClonable - Supports object copying
5. IEquatable - Allows comparing to object instances.
6. IFormatable - Provides way to convert the value of object into a formatted string. This is similar to ToString() method, but provides greater flexibility than it.

Interfaces

Interface are a type similar to class, where it cannot have the Method Content, rather it has the definition of Method. it will not have properies and members variables. the class which inherits the interface should implement all the methods of the interface. the interface forces the derived class to implement, otherwise it will not get compiled. The below lists ome of common interfaces of .Net
1. IComparable - Implemented by ttypes whose value can be ordered, it is ued for sorting.
2. Idisposable - Defines this method to dispose of a object manually, this is important most commonly used Interface when dealing with more big objects, and also to release the object which holds the resource like database.
3. IConvertible - Enables the class to to base type such as string, int or bool.
4. IClonable - Supports object copying
5. IEquatable - Allows to compare to object instances.
6. IFormatable - Provides way to convert the value of object into an formatted string. This is similar to ToString() method, but provides greater flexibility than it.

Nullable type in .Net 2.0

Nullable type allows a varible to store null value, Infact this is the special about the type. The main purpose is it will add two members to the variable HasValue and Value Members. for example, if you store data for a yes/no question and if the user did not answer the question then null will be stored in that place. This means it stores True, False and third state nothing as null also. This will be represented as shown below.

In VB.Net,
Dim b as Nullable(Of Boolean) = Nothing
.....
If b.HasValue Then
Do if b has some value and not null
Else
Do if b is null
End If
In C#,
Nullable b = null;
Otherwise it can be used this way also, only applicable for C#,
bool? b = null;
.....
if (b.HasValue) {Do if it is not null} else {Do if it null}

Optimizing performance with built-in types .Net

The runtime optimizes the performance of 32-bit integer types (Int32 and UInt32), so use those types for counters and other frequently accessed integral variables. For floating-point operations, Double is the most efficient type because those operations are optimized by hardware.

ImageMap Control - ASP.Net 2.0

ImageMap is a New WebControl of ASP.Net 2.0. this is similar to the age old HTML control Map. This enables to set regions in a single image, and also sets a actions for that region. Means, if the region is clicked it will navigate to some other page or site, submits the page. A region can be created in Rectangle, polygan or in circle. The syntax is shown below with a Example.

<asp:ImageMap ID="ImageMap1" runat="server" HotSpotMode="PostBack" ImageUrl="~/Images/spaces.gif" OnClick="ImageMap1_Click" >
<asp:CircleHotSpot Radius="30" PostBackValue="Circle" AccessKey="c" AlternateText="some text" HotSpotMode="PostBack"
NavigateUrl="~/Trial.aspx" TabIndex="1" Target="_blank"/>
<asp:PolygonHotSpot Coordinates="0,0;0,30;30,30;30,0;" PostBackValue="Polygon" />
<asp:RectangleHotSpot Bottom="20" Right="20" PostBackValue="Rectangle" />
</asp:ImageMap>

protected void ImageMap1_Click(object sender, ImageMapEventArgs e)
{
Response.Write(e.PostBackValue.ToString());
}


The Properties used in the controls are listed:

PostBackValue - this value can be retrived by the EventArgs of ImageMap Click Event(e.PostBackValue).
AccessKey - combination alt + Specified Key makes the event triggered.
AlternateText - text in the region specified if image is not available.
HotSpotMode - decides the behavior of the event, listed below are the description of this Mode
> NotSet - No HotSpot set
> Inactive - Region set and restricted to stop firing the event.
> Navigate - Navigates to the url set by the NavigateUrl property.
> PostBack - Submits the page with PostBackValue set, triggers the Click Event.
NavigateUrl - set to get navigated to the specified URL Value, when triggered.
Target - Open the redirected url in specified target(Like same window or new window).

Image Map supports three types of region specification. Apart from the properties, the HotSpot region specifies will have a shape oriented property like Circle has radius, x and y; Rectangle has top, left, bottom and right; polygan has coordinates. a single Image map can have any no. of HotSpot's in any combinations. if same region is mention twice then the priority goes for the first specified one. the HotSpot is basically a collection, all these propeties can be set by design mode also using Property Window.

The HotSpotMode can be set in Imagemap Tag, so that the same will be applicable for all, if required it can be specified in each individual HotSpot. The PostBackValue can be set for each HotSpot, that enable to identify the region get triggered, when HotSpotMode is PostBack. the EventArgs gives the PostBackValue.