Sunday, October 19, 2008

Modify Web Configuration file without restarting asp net worker process (aspnet_wp).

Modify Web Configuration file without restarting asp net worker process (aspnet_wp).

No one in the world can say that web.config file will remain intact after the deployment. Modifications have to happen to some or the other parameters of the web configuration file like one has to add or remove key value pairs from appsettings section etc…

So what?
As soon as the webs configuration file is saved the aspnet_wp worker process of that particular web application gets restarted and all the current sessions and data are lost.

This can cause tremendous issues to an organization whose application needs to be up for 24*7.

To recover from such kind of scenario Microsoft has already provided a great tweak to handle this.

Following are some steps that need to be followed.
Step 1) Make a separate configuration file lets say for example appsettings.config with the following xml data.

[sourcecode language='html']



 
 
 


[/sourcecode]

Step 2) Now in the original web.config file make your appSettings section look like below

[sourcecode language='html']




[/sourcecode]

You are done.

 
Place a debug point on the Application_Start event in global.asax file and check that changing appsettings.config does not restart the worker process.

Saturday, August 30, 2008

How to create Scheduled Jobs in .net web applications.

How to create Scheduled Jobs in .net web applications.

Calling a function at predefined iteration of time has been one of the key requirements of web applications.
Example:
You want to automatically archive the data.
You want to automatically purge the data.
You want to automatically send the News Letters.
You want to take backup of the files or database etc.

Generally this type of functionality can be easily archived by creating and configuring scheduled jobs in SQL Server.

What if you are using the express edition of SQL Server?
You cannot take advantage of this feature because all the editions of SQL Server do not provide this functionality.

In this article I will explain how we can achieve this functionality using purely .Net framework class.

First of all let’s thanks System.Threading class. The main cream lies inside this class shipped in .Net Framework. Many of us usually do not use it but believe me its one of the most beautiful classes of the framework.

Anyways

Following are the steps which you need to perform:

Step 1) Create a class called Jobs.cs

[sourcecode language='csharp']

namespace myScheduler
{
    public sealed class Jobs
    {
        public Jobs(){}

        public static void DailyJob(object state)
        {
           //Your dream goes here.
        }

        public static void HourlyJob(object state)
        {
            //Your dream goes here.
        }
    }
}

[/sourcecode]

Jobs class includes the below two functions that will be called by our scheduler which we will create in the second step.
I have created two functions for this purpose.
DailyJob: Will be called daily by the scheduler.
HourlyJob: Will be called hourly by the scheduler.

 

Step 2) Create a class called Scheduler.cs

[sourcecode language='csharp']

using System;
using System.Threading;

namespace myScheduler
{
    public sealed class Scheduler
    {
        public Scheduler() { }

        public void Scheduler_Start()
        {
            TimerCallback callbackDaily = new TimerCallback(Jobs.DailyJob);
            Timer dailyTimer = new Timer(callbackDaily, null, TimeSpan.Zero, TimeSpan.FromHours(24.0));

            TimerCallback callbackHourly = new TimerCallback(Jobs.HourlyJob);
            Timer hourlyTimer = new Timer(callbackHourly, null, TimeSpan.Zero, TimeSpan.FromHours(1.0));
        }
    }
}

[/sourcecode]

Scheduler class contains the actual mechanism for running jobs created in Jobs class in Step one.

Timer: Mechanism for executing method at specified time intervals. This method has 5 overloads which we can use as per our needs. Some of its parameters are
TimerCallback: Represents the method that you want to actually callback on at a particular interval of time.
State: Any type of object you want to pass to your callback method.
Due Time: Amount of delay after which callback will be invoked. TimeSpan.Zero means immediately.
Period: Time period between invocations of callback function.

 

Step 3) Add Global.asax file to your web project. In Application_Start event instantiate Scheduler object and make a call to Scheduler_Start() method.

[sourcecode language='html']

<%@ Application Language="C#" %>
<%@ Import Namespace="myScheduler" %>



[/sourcecode]

 

You are done.

How it works?
Whenever the applications is first started the  Application_Start() in Global.asax is called and this calls Scheduler_Start() method which configures all the Jobs that we have created.

I have demonstrated a daily and hourly job in the above examples. In order to debug and test this code you need to wait for an Hour or a Day. Don’t panic I have done this intentionally. So in order to brush up your above learning’s prepare a job in Jobs.cs file that would be called every minute. Configure the callback and timer for your new job in Scheduler.cs file and enjoy the gist of scheduling.

Note: After running the project once do not forget to restart the IIS because Application_Start() wont be called every time you run your project. You know why?

Friday, August 15, 2008

How to Encrypt or Decrypt sections of Configuration file.

How to Encrypt or Decrypt sections of Configuration file.

It is always a good practice to protect sensitive information in configuration files. One should always protect connectionStrings section from being easily readable; also some times it becomes necessary to encrypt appSettings section that may include some sensitive data. You know why…?

So in this Blog I will walkthrough the steps required for encryption\decryption of sections in configuration file.

.Net framework ships two providers for this purpose.
RsaProtectedConfigurationProvider and DpapiProtectedConfigurationProvider.
Here I will use RsaProtectedConfigurationProvider.

 
Step 1) First off all we will add provider to configuration section of web.config file that you want to play with. This provider is required for actual processing done while running commands depicted in step 3 and 4.

[sourcecode language='html']




 
    type="System.Configuration.RsaProtectedConfigurationProvider"
  keyContainerName="NetFrameworkConfigurationKey"/>




[/sourcecode]

 RsaProtectedConfigurationProvider requires key container for encryption and decryption purpose. This container includes private\public keys that are required during encryption and decryption process by the attached provider.

NetFrameworkConfigurationKey : This is the default key container shipped by Microsoft. You can also create your own key container that includes private\public keys required for encryption\decryption. In order to avoid confusion I will depict these steps in my next article.

Note: As we are using default key container "NetFrameworkConfigurationKey" you can do away with the first step. This step will be added by machine.config file cause it includes declaration for both providers  ("RsaProtectedConfigurationProvider" and "DpapiProtectedConfigurationProvider"). But this will be helpful incase we are creating our own key container.



Step 2) After adding the provider in Step 1 go to the Visual Studio command prompt. Now In order to access "NetFrameworkConfigurationKey" key container your "ASPNET" account must have permission to access it. Run the below command to give access.

aspnet_regiis -pa "NetFrameworkConfigurationKey" "ASPNET"

 

Step 3) Run below command to encrypt connectionStrings section of your web.config file located in the virtual directory named "MyWebApplication".

aspnet_regiis -pe connectionStrings -app /MyWebApplication

 

Step 4) Run below command to decrypt connectionStrings section of your web.config file located in the virtual directory named "MyWebApplication".

aspnet_regiis -pd connectionStrings -app /MyWebApplication

Note: You can replace the "connectionStrings" section with the section name you want to encrypt\decrypt.
Eg: aspnet_regiis -pe appSettings -app /MyWebApplication
This will encrypt "appSettings" section of your web.config file located in the virtual directory named "MyWebApplication".

To get more help about aspnet_regiis use the below command
aspnet_regiis help

Saturday, July 19, 2008

Creating Custom Controls in ASP .Net

Creating Custom Controls in ASP .Net

In this article I have simply demonstrated 2 code examples for creating Custom Web Controls.

Why Custom Controls?
To cater through the real world scenarios that may not be supplemented by inbuilt .Net controls, the need for Custom Control arises. Also their are several other reasons for creating custom control like reusability, centralized management over the control, ease of usage etc.

How to approach the requirement for creating Custom Controls?
If any of the inbuilt .Net control meets your requirement than you should always use these controls. If not than ask yourself a question

Would any of the existing control help me?

If yes than
Inherit from the existing control and add the new features to it and extend it further.
If No than
You need to build the control from scratch. In this case inherit from WebControl class.

 

How to create Custom Controls?

Their are two ways in which you can create a custom control.
1) You can inherit from existing .net web control to extend its features further. OR
2) You can directly inherit from WebControl class to create a web control from scratch.

Depending upon the approach you choose their will be several properties and methods for you to override. All of them cannot be explained here so I am explaining some important among them that I have used in my code samples.

AddAttributesToRender - Adds HTML or Style attributes for any of your HtmlTextWriterTag. You can also use HtmlTextWriter's "AddAttribute" , "AddStyleAttribute" methods to achieve the same functionality. Make sure that you add this attributes before using "RenderBeginTag" method.

RenderContents - Outputs HTML content of Server Control to HtmlTextWriter object. So override this method to manage the HTML that your custom control renders to the browser.

TagKey - WebControl renders a span TAG by default on the cient browser. But to render any specific TAG you need to override this method.

Inheriting from the existing control
This example depicts the creation of a new web control named "myButton" that is derived from the Button class. It has all the features that a normal button control has. But for a twist I have added a simple feature that prompts for a message before submitting the page back to the server. I have added a new property called "AlertMessage" to quench this requirement.

[sourcecode language='csharp']

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace myControlLib
{
public class myButton : Button
{
public myButton() { }
private string _AlertMessage = "Are you sure to process this request!";
public string AlertMessage
{
set { _AlertMessage = value; }
get { return _AlertMessage; }
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "return confirm('" + _AlertMessage + "')"); base.AddAttributesToRender(writer);
}
}
}

[/sourcecode]

Registering the control on the web page

[sourcecode language='html']

<%@ Register Assembly="myControlLib" Namespace="myControlLib" TagPrefix="myUcl" %>

[/sourcecode]

Using the control

[sourcecode language='html']





[/sourcecode]





Inheriting from the WebControl class
This example depicts the creation of a new web control named "myImage" that is directly derived from the WebControl class. This control renders a Picture, Name of the Picture and Description of the Picture. Description of the Picture is displayed when the mouse is moved over the picture.
For this I have created 3 additional properties for this.

ImageUrl - The url of the image to be displayed

ImageName - The name of the image.

ImageDescription - The description for the image.

[sourcecode language='csharp']

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace myControlLib
{
public class myImage : WebControl
{
public myImage() { }

private string _ImageUrl = "";
public string ImageUrl
{
get { return _ImageUrl; }
set { _ImageUrl = value; }
}
private string _ImageName = "";
public string ImageName
{
get { return _ImageName; }
set { _ImageName = value; }
}
private string _ImageDescription = "";
public string ImageDescription
{
get { return _ImageDescription; }
set { _ImageDescription = value; }
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.AddAttribute("onmousemove", "javascript:document.getElementById('" + this.UniqueID + "_divDescription').style.display='';");
writer.AddAttribute("onmouseout", "javascript:document.getElementById('" + this.UniqueID + "_divDescription').style.display='none';");
writer.AddAttribute(HtmlTextWriterAttribute.Src, _ImageUrl);
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.Write("Name : " + _ImageName);
writer.RenderEndTag();
writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");
writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID + "_divDescription");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.Write("Description : " + _ImageDescription);
writer.RenderEndTag();
base.RenderContents(writer);
}
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Div;
}
}
}
}

[/sourcecode]

Registering the control on the web page

[sourcecode language='html']

<%@ Register Assembly="myControlLib" Namespace="myControlLib" TagPrefix="myUcl" %>

[/sourcecode]

Using the control

[sourcecode language='html']

ImageDescription="My Image Description........" />

[/sourcecode]

Note: The above examples are the simplest controls that one can create. Remember that you can create more complex controls using the same approach. Choosing the best control that can serve the purpose of the requirement is our JOB and so make this decision very carefully.

How to make better appearance of your custom control in Visual Studio.

How to add custom control in Visual Studio Toolbox.

How to make better appearance of your custom control in Visual Studio.

How to make better appearance of your custom control in Visual Studio.

In the previous article Creating Custom Controls in ASP .Net I have covered the technical approach for creating custom control.

Here I will explain how to improve the appearance of your custom control
Their are two ways in which this job can be done.
1) By decorating your control and control properties with some design time attributes.
2) By making your own control designer.

Decorating your control and control properties with some design time attributes:
.Net provides various design time attributes that can be applied on control or on property to take control over the design aspects of the control. You can do lots of things with this attributes let’s say:

One can choose the default event or default property for their controls.

One can assign the description to the property that is displayed in the last section of the property window when the property is selected.

One can categorize their additional properties in property window. This would be helpful when the property window is switched to "Categorized" mode.Etc...

Their are lots of such attributes with which you can playaround.
Some of them I have described below.

Attributes you can apply to your control class:

DefaultEvent - This attribute specifies the default event for your control. So when a control is double clicked, the event handler is automatically created for this event.

DefaultProperty - This attribute specifies the default property for your control. So when you open the property window for your control, this property will be focused by default.

ToolboxData - This attribute allows you to specify any tags that are added to the markup of your control whenever it is added to the page.

ToolboxItem - This attribute allows you to block the control from appearing in the toolbox.

TagPrefix - This attribute allows you to manage the html markup for the registration tag of your control.

Attributes you can apply to your control property:

Bindable - Displays a data binding dialog box for the property.

Browsable - This attribute allows you to block the property from appearing in Property window.

Category - This attribute allows you to group properties in a particular stencil. The property will appear under this category in the property window.

DefaultValue - This attribute allows you to give default value for the property.

Description - This attribute allows you to specify the description for the property. This description is displayed below in the property window when the property is selected.

Editor - This attribute allows you to specify the custom editor window for the property. Examples of custom editor are ImageUrlEditor, MailFileEditor,  MdbDataFileEditor,  UrlEditor etc...

EditorBrowsable - This attribute allow you to block a property from being display in the Intellisense.


Example Source Code
private string _ImageUrl = "";
[Category("My Custom Properties")]
[Description("Specify path of the image to be displayed.")]
[Editor("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[DefaultValue("")]
[UrlProperty]
[Bindable(true)]
public string ImageUrl
{
    get { return _ImageUrl; }
    set { _ImageUrl = value; }
}
Making your own control designer:
Control Designer help to take control over the appearance of the control when they are drag dropped in the page. One can also create a smart tag that can help user to easily configure some properties of your control. Example: Some of the .Net controls like FormView, DetailsView, GridView have smart tags that help users to easily configure some important properties like Datasource, Templates etc. I will try to cover How to make smart tag controller in my next article.

Example Source Code
using System;

using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;


[assembly: TagPrefix("myControlLib", "myUcl")]
namespace myControlLib
{
public class myImageDesigner : ControlDesigner
{
myImage objmyImage;
public override string GetDesignTimeHtml()
{
if (objmyImage.ImageUrl.Trim().Length == 0)
{
return "Please set Image URL Property!";
}
else
{
return base.GetDesignTimeHtml();
}
}
public override void Initialize(IComponent component)
{
objmyImage = (myImage)component;
base.Initialize(component);
return;
}
}
[Designer("myControlLib.myImageDesigner,myControlLib")]
[ToolboxBitmap(typeof(myImage), "myImageLogo.bmp")]
[ToolboxData(@"<{0}:myImage runat=""server"" ImageUrl="" "" ImageName="" "" ImageDescription="" "" />")]
public class myImage : WebControl
{
public myImage() { }
private string _ImageUrl = "";
[Category("My Custom Properties")]
[Description("Specify path of the image to be displayed.")]
[Editor("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[DefaultValue("")]
[UrlProperty]
[Bindable(true)]
public string ImageUrl
{
get { return _ImageUrl; }
set { _ImageUrl = value; }
}
private string _ImageName = "";
[Category("My Custom Properties")]
[Description("Specify name of the image to be displayed.")]
public string ImageName
{
get { return _ImageName; }
set { _ImageName = value; }
}
private string _ImageDescription = "";
[Category("My Custom Properties")]
[Description("Specify description of the image to be displayed.")]
public string ImageDescription
{
get { return _ImageDescription; }
set { _ImageDescription = value; }
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.AddAttribute("onmousemove", "javascript:document.getElementById('" + this.UniqueID + "_divDescription').style.display='';");
writer.AddAttribute("onmouseout", "javascript:document.getElementById('" + this.UniqueID + "_divDescription').style.display='none';");
writer.AddAttribute(HtmlTextWriterAttribute.Src, _ImageUrl);
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.Write("Name : " + _ImageName);
writer.RenderEndTag();
writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");
writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID + "_divDescription");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.Write("Description : " + _ImageDescription);
writer.RenderEndTag();
base.RenderContents(writer);
}
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Div;
}
}
}
}


Note: Do not forget to add a .bmp image named ("myImageLogo.bmp") to this project. This image would be displayed as the icon for your control in the toolbox. You can also create .bmp file using inbuilt .Net design tool. Just click on the added .bmp file to open this editor.
Your bmp should be 16x16 pixels in size.


To add custom control to your toolbox refere
How to add custom control in Visual Studio Toolbox.

How to add custom control in Visual Studio Toolbox.

How to add custom control in Visual Studio Toolbox.

In my previous article Creating Custom Controls in ASP .Net  we have seen the technical approach for creating custom control and  in How to make better appearance of your custom control in Visual Studio we have gone through the design aspects of custom control.

Now in this article I will explain how to add your custom control to your Toolbox.


Making Custom Control Class Library Project.

Step 1 - Create a class library project named myControlLib.

Step 2 - Add the following code to a new class named myImage. To download sample code files click here. After downloading add “myImage.cs” and “myImageLogo.bmp” file to your “myControlLib” project. Change the “Build Action” property of “myImageLogo.bmp” to Embedded Resource.

Step 3 - Compile the project.

Note: Above given steps are specific to the code sample that I have created. If you want to rename any of the file or change any other aspects than make sure that you also change the design time attributes of your control.

Example: [Designer ("myControlLib.myImageDesigner,myControlLib")] uses the namespace “myControlLib” that is directly related to the name of the class library project you make.


Adding custom control to toolbox.

Step 1 - Switch over to the web project in which you want to use the control.

Step 2 - In toolbox add your own tab or click on general tab.

Step 3 - Right click in the selected tab area and click Chose Items.



Step 4 - This will open Choose Toolbox Item screen. Click on Browse button and specify the path of dll you created in your myControlLib project.





Step 5 - Click Ok and you’re done. Your Custom control will be added to the toolbox as shown below.



Saturday, July 5, 2008

Application Caching in .Net 2.0

Application Caching in .Net 2.0

What is Application Caching?
Application Caching is a technique in .Net 2.0 for caching different type of objects for faster access. As the name itself depicts, the Cache Object can be used from the entire application. A single cache object lies for the entire application and so can be used between various different user sessions and requests.

How Application Caching works and How to use it?
The main motive for Application Caching is to store cached Objects in memory for faster access. Expensive operation to obtain and persists data or information can lead to application failure or sluggishness at time of heavy traffic. Also the amount of time and resources involved in such operations may not be satisfactionary. So to improve upon the performance and the scalability of your application you can cache the expensive data for faster access.

You can use either Cache.Add() or Cache.Insert() methods for caching your data. The only difference between the two is, Cache.Add() method returns the object which you want to cache.
So let’s say if you want to use the object and cache it as well. You can do so in a single line of code with the help of Cache.Add().

Cache.Insert() methods has 4 different types of overloaded methods while Cache.Add() has only one.



Different Parameters used for the above methods:

Key
It is the name for the cached object by which it will be accessed. You can also use the key to remove the cached object from the memory. Following code will remove the cached object named “customerList” from the memory.

Cache.Remove("customerList");


Object to be cached
It is the object that you want to cache. This object will be stored in the memory for faster retrieval.


Dependency
It is the CacheDependency object that can be a file or cache key. So if the file or cache key is changed than the object in the cached will be invalidated and removed from the cache. This can be used to maintain the freshness of the data. This is an optional parameter. You can set it to null. Incase of multiple dependency conditions you can use AggregateCacheDependency object instead of CacheDependency.


Absolute Expiration DateIt is the time at which the object will be removed from the cache. To remove the object from cache after 10 minutes you can set this parameter to DateTime.Now.AddMinutes(10) as shown in the below code.

Cache.Insert("customerList", objDataSet, new CacheDependency(Server.MapPath("~/App_Data/customerList.xml")),DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration,CacheItemPriority.Normal, null);


If you do not want the object to expire forcefully after some time than you may set this parameter to Cache.NoAbsoluteExpiration as shown in below code.
Cache.Insert("customerList", objDataSet,new CacheDependency(Server.MapPath("~/App_Data/customerList.xml")),Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,CacheItemPriority.Normal, null);

Time Span
It is the time span after which the object will be removed from the cache if it was not accessed. So lets say if you want to remove the object from cache if it has not been accessed since last 10 minutes than you can use the below code.

Cache.Insert("customerList", objDataSet,new CacheDependency(Server.MapPath("~/App_Data/customerList.xml")),Cache.NoAbsoluteExpiration,DateTime.Now.AddMinutes(10),CacheItemPriority.Normal, null);


If you do not want the object to expire forcefully after some time it was last accessed than you may set this parameter to use Cache.NoSlidingExpiration  as shown in below code.
Cache.Insert("customerList", objDataSet,new CacheDependency(Server.MapPath("~/App_Data/customerList.xml")),Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,CacheItemPriority.Normal, null);

Note: If you are using this sliding expiration time parameter than Absolute Expiration Date parameter must be Cache.NoAbsoluteExpiration

Priority
It is the priority at which the objects will be purged from the memory incase if the memory runs low. Lower priority objects will be removed first.

Callback Method
It is the event handler that will run when the object is removed from the cache. If you do not want to use this parameter than you can set this to null.

To download Application caching code sample Click Here.
Note: To introduce sophisticated caching layer in your application you can always use Microsoft’s Enterprise Library - Caching Application Block.

Friday, June 20, 2008

Understanding Globalization and Localization in .NET

Understanding Globalization and Localization in .NET

Now days as the audience for web applications has bursted from computers to pda's, cell phones and many other gadgets, Globalization has become must for web application. Globalization allows your web application to be useful for different people in different parts of the world who speaks different languages. Thanks to Microsoft .Net Globalization feature. Using this feature one can easily build a web application available to dispersed audience throughout the world. In this article I will walkthrough the steps that are essential for creating a multi cultured application. This example will include the use of Global and Local resource files that are heart for multi cultured web application. ASP.Net supports two types of resources Local Resource and Global Resource. Both types of Resources are xml based resource files that can contain text translated into particular language

 Local Resources: Local resource is specific to particular webpage. All the resource files related to particular webpage must be placed in a special .Net folder named App_LocalResources. This folder must be in the same folder of the webpage that you want to localize. So App_LocalResources can be subfolder for any folder of your website.

 Global Resources: They are the resource files for the entire web application. They are not specific to any page. They can be accessed from the entire web application. Global resource files must be located in a special .Net folder named App_GlobalResources. App_GlobalResources must be at the root of the web application.

 Steps to create local Resource file.
1) Switch to design view of the webpage for which you want to create the resource file.
2) Go to Tools menu and click Generate Local Resource.
3) As the result of step 2 .Net generates the .resx file. Naming conventions that Resource files follow is as explained below: <PageName>.aspx.<language>.resx Example myWebPage.aspx.resx: This is the default resource file of your page. If no culture matches than this resource file is used. myWebPage.aspx.es.resx: For Neutral Spanish culture it may look like this. myWebPage.aspx.es-MX.resx: For Specific Spanish Mexican culture it may look like this. What do you mean by neutral and specific cultures? I leave this small exercise to readers.

 Steps to create global Resource file.
1) Their are no specific utilities in .Net to generate Global resource files automatically. But you can create App_GlobalResources folder at root level and add a resource file manually Or you may copy the local resource file and change it at your will.  

Ways in which you can Globalize or localize your application. Implicit Localization
<asp:Label ID="lblLoadLocalResource" runat="server" 

Text="Local Resource" 

meta:resourcekey="lblLoadLocalResourceResource1">

</asp:Label>

Implicit Globalization
<asp:Label ID="lblLoadGlobalResource" runat="server" 

Text="<%$ Resources:Resource, commonHeader %>" 

ToolTip="<%$ Resources:Resource, commonHeader %>">

</asp:Label>

Programmatic Localization
lblLoadLocalResource.Text = GetLocalResourceObject
("lblLoadLocalResourceResource1.Text").ToString();

lblLoadLocalResource.ToolTip = GetLocalResourceObject
("lblLoadLocalResourceResource1.ToolTip").ToString();

Programmatic Globalization
lblLoadGlobalResource.Text = GetGlobalResourceObject
("Resource", "commonHeader").ToString();

lblLoadGlobalResource.ToolTip = GetGlobalResourceObject
("Resource", "commonHeader").ToString();

How to change the UI Culture? Steps to check and configure the UI Culture of your webpage 1) To change it programmatically you need to override the InitializeCulture() of your web page.
protected override void InitializeCulture()
{
 UICulture = "en";
}
2) To change it implicitly from .aspx page Set
<%@ Page Language="C#" UICulture="en" %>
3)To change it globally from web.config file
<system.web>
 <globalization uiCulture="es"/>
</system.web>
 
Note: If you set UICulture="auto" then .Net runtime will automatically detect the requesting browsers preferred language setting and will compile the related resource file for that culture. In this case you do not have to write a single piece of code for setting UICulture.

 How to check this?
1) Open IE and go to Tools > internet Options
2) Click Languages tab.
 3) This will open language Preference dialog box window.
4) Add the language by clicking the add button. Make sure to add the language for which you have configured the resource file of your web page.
5) Select the language that you have added and using Move Up button, bring it to the first topmost position in the language list.
 6) Press Ok and again Ok to return. Open your webpage in the browser and enjoy the taste of globalized web application. I believe that few lines of code can speak thousand words and so I have prepared a small demo that includes both Globalization and Localization.  

To download the entire source code Click Here.  

Note: Globalizing the web application entirely, requires lots of other things like changing the Culture, formatting dates and currency and lot more. I will try to incorporate all this stuff in my next article.

Friday, June 13, 2008

Implementing ICallbackEventHandler in .Net 2.0

Implementing ICallbackEventHandler in .Net 2.0 Custom Control.

In this article I depict the use of ICallbackEventHandler with an example that shows random text generated from the server side code. For this purpose I have created a custom control named Show Tip.

How Show Tip control works.
Show Tip control renders the HTML button. Clicking this button initiates AJAX call to the server. The server processes the AJAX call raised from the client browser and returns the result. Lastly a JavaScript registered on the client browser displays the result in a DIV tag. The main motive behind this is to avoid the full page post backs. Show tip Button directly communicates with the server using "Sneaky Post back".

How callbacks also known as "Sneaky Post back" actually work.
The main magic lies in the ICallbackEventHandler interface.By implementing this interface and some lit bit of code one can easily trap the events raised from Client Browser, on the server and can respond to such events.



Steps to use ICallbackEventHandler .

Step 1)Render the JavaScript on Client Browser that will initiate the AJAX call.
To create the JavaScript that initiates the AJAX call use Page.ClientScript.GetCallbackEventReference() method.

This method returns a string
WebForm_DoCallback('ShowTip1',null,showResult,null,showError,false).

This is the JavaScript function that is used to callback to the server.

The parameters used for GetCallbackEventReference() method are listed below.
1)  control - the control that initiates the AJAX call.

2) argument - the argument that is send to the server. In this example it is null as we are not passing any value back to server. But you can pass any value Example if you want to display price for the selected item from combo.

3) clientCallback - the name of the JavaScript function that executes after the result is returned from the server.

4) context - the argument that is passed back to the clientCallback() and clientErrorCallback() methods after the AJAX call completes . In this example it is null as we are not passing any value to client browser.

5) clientErrorCallback -  the name of the JavaScript function that executes on client browser if an error on the server occues during AJAX callback.

6) useAsync - pass false to perform the AJAX call asynchronously.



Step 2)Implement the methods of ICallbackEventHandler to handle the AJAX call.
ICallbackEventHandler has two methods that need to be implemented on the server side.
1) RaiseCallbackEvent - This method is called first on server by AJAX call from the client.
2) GetCallbackResult - This method Returns the result of the AJAX call to the client.



Step 3)Render the JavaScript on Client Browser that manipulates the result received from the server. To render the JavaScript on Client Browser use Page.ClientScript.RegisterClientScriptInclude("ShowTip", Page.ResolveClientUrl("~/ClientScripts/ShowTip.js")) method.
The parameters used for RegisterClientScriptInclude() method are listed below.
1) key - any string that represents the script instance name.
2) url - the path of the JavaScript file to register on the client browser that is called when results are returned from the server.



To download the entire example Click Here


ASP .Net 2.0 bundles some controls like Grid View, Tree View and Details View that use AJAX callbacks to perform some special tasks like
Grid View uses callback to perform AJAX based paging and sorting.
Tree View uses callback to perform AJAX based expansion of nodes.
Details View uses callback to perform AJAX based paging.
You will have to enable some property to do so. I leave this small exercise to the readers.

Thursday, June 5, 2008

How to Paste HTML content in blogspot?

Problem in pasting HTML content in your blog?

This is because all the blogs does not allow to directly use html in their content area due to security reasons.

The area for writing the content in Blogspot is commonly known as What You See Is What You Get (WYSIWYS) editor.

This WYSIWYG editor interprets the pasted HTML as true HTML. It will render exactly whatever you paste in it as HTML. This might spoil the look and feel of your blog.

Why this happens?

Special characters used for HTML language like <,>,",& etc should be treated in special manner.
Example:
 < can be treated as <
 > can be treated as >
 " can be treated as "e;
 & can be treated as &

So writing these special characters in this manner and pasting it in blog can solve your problem. But this may be a tedious task to perform manually.

So here is the solution for it.

Step 1) After making the below code working in .Net 2.0 copy the HTML you want to put in your blog and paste in the text area.

Step 2) Click Encode HTML button.

Step 3) 2nd step will generate the encoded html below the button.

Step 4) Copy the output from 4th step to your blog content (WYSIWYG) editor.


Following sample code converts HTML to Encoded HTML format using Code behind Modal in .Net.

<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>">
<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>">
<head runat="server">
    <title>Encode HTML</title>
</head>
<body>
    <form id="form1" runat="server">
<div>
           <asp:TextBox ID="txtHTML" runat="server" Columns="75" Rows="10" TextMode="MultiLine"></asp:TextBox>     
           <asp:Button ID="btnEncode" runat="server" Text="Encode HTML" OnClick="btnEncode_Click" />
           <asp:Literal ID="litEncodedHTML" runat="server" Mode="Encode"></asp:Literal></div>
    </form>
</body>
</html>

using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; 
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    { 
 
    } 
 
    protected void btnEncode_Click(object sender, EventArgs e)
    {
        litEncousing System;dedHTML.Text = Server.HtmlEncode(txtHTML.Text.Trim());
    }
} 

Note: Those who are unaware of .Net they can use the same logic with their know language. The main crux here is Server.HtmlEncode() method which transforms the HTML to encoded HTML.

Wednesday, June 4, 2008

How to Freeze Webpage while it is being processed in .Net 2.0 usingAJAX?

How to Freeze Webpage while it is being processed in .Net 2.0 using AJAX?
Blocking a webpage seems to be difficult task while the data on the page is being posted back to the server or it is being refreshed. It becomes tricky to track the postback and reloading of the page. Though this can be achieved by using Response.Flush() method but it requires some extra effort.

But thanks to AJAX Update Progress Panel.

Using Update Progress Panel its just the mater of putting it on the page. The real magic lies in this panel and the DIV tag inside it. The panel takes care of the events required during the page updation and the DIV blocks the page till the panel is active. The magic that actually blocks the page lies in the DIV tag. I leave this to readers to find the crux of the magic.

Following is the Code you can use to block the Webpage.


<div style="top: 0px; height: 1000px; background-color: white; opacity: 0.75; filter: alpha(opacity=75); vertical-align: middle; left: 0px; z-index: 999999; width: 1000px; position: absolute; text-align: center;"></div>



I have used simple DIV tag but it can be enhanced using an animated gif file or other custom JavaScript for showing loading image or processing time.

Note:If you are using this code on the local server than AJAX might be so fast that the effect of this may not be visible.
So try to use this code where the server requires some time to process the request OR use Thread.Sleep(5000) on your page.

Sample Code Using Code Behind Modal:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WaitForRequest.aspx.cs"
    Inherits="WaitForRequest" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="ajax" %>
<!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>Wait For Request</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <ajax:ScriptManager ID="ScriptManager1" runat="server">
        </ajax:ScriptManager>
        <ajax:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Button ID="btnGetDate" runat="server" Text="Get Date" OnClick="btnGetDate_Click">
                </asp:Button>
                <asp:Label ID="lblTime" runat="server"></asp:Label>
            </ContentTemplate>
        </ajax:UpdatePanel>
        <ajax:UpdateProgress ID="UpdateProgress1" runat="server">
            <ProgressTemplate>
                <div style="top: 0px; height: 1000px; background-color: White; opacity: 0.75; filter: alpha(opacity=75);
                    vertical-align: middle; left: 0px; z-index: 999999; width: 1000px; position: absolute;
                    text-align: center;">
                </div>
            </ProgressTemplate>
        </ajax:UpdateProgress>
    </div>
    </form>
</body>
</html>


using System;
using System.Threading;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class WaitForRequest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnGetDate_Click(object sender, EventArgs e)
    {
        Thread.Sleep(5000);
        lblTime.Text = DateTime.Now.ToString();
    }
}