Toars I will smile to the world ^o^

11Aug/090

How to have customized ResourceProvider and ResourceProviderFactory

It is not flexible to use the default ResourceProvider to access the resources. Different applications have different requirements, such as storing the resource files in another folder other than the default App_GlobalResources, having shorter naming conventions, or other purposes.

Thus, it would be much better and flexible to have our own customized ResourceProvider.

I create two classes in my project, ResourceProvider.cs and ResourceProviderFactory.cs, where ResourceProvider implements the interface System.Web.Compilation.IResourceProvider, and ResourceProviderFactory overrides System.Web.Compilation.ResourceProviderFactory.

In the ResourceProviderFactory.cs file:

namespace Web.Core
{
    public class ResourceProviderFactory: System.Web.Compilation.ResourceProviderFactory
    {
        public override IResourceProvider CreateGlobalResourceProvider(string classKey)
        {
            return new ResourceProvider();
        }
    }
}



In the ResourceProvider.cs file:

namespace Web.Core
{
    public class ResourceProvider: IResourceProvider
    {
        private ResourceManager resourceManager;

        public void ResourceProvider(string defaultNameSpace, string folder)
        {
            if (resourceManager == null)
                resourceManager = new ResourceManager(defaultNameSpace, Assembly.Load(folder));
        }

        public object GetObject(string resourceKey, CultureInfo culture)
        {
            string value = resourceManager.GetString(resourceKey, culture);

            if(string.IsNullOrEmpty(value))
                return "Do Something Here";
            else
                return "Do Something Else Here";
        }
    }
}



In the Web.Config file: add the section below to <system.web>

<globalization resourceProviderFactoryType="Web.Core.ResourceProviderFactory, Web.Core"
               enableClientBasedCulture="true" uiCulture="auto" culture="auto" />


11Aug/091

How to make ASP.NET resources strongly typed

In multi-language supported applications, for different languages, we need to have different localized resource files, such as Common.resx (the default fallback resource file) and Common.en-GB.resx (the language specific resource file).

If we don't override the default classes ResourceProvider and ResourceProviderFactory, we have to store all the resource files either in the App_GlobalResources folder or App_LocalResources folder. Or even we have our own resource provider, we still need to access the resources using this way in Aspx pages:

<%$ Resources:Common, LblYourName %>



Or access the resources in the code-behind pages:

ResourceProvider rp = new ResourceProvider();
rp.GetObject(resourceName, culture);



The biggest disadvantage of accessing resources in those ways is you do not have the intellisense nor the compile time error hint.

In order to avoid this, we can make the resources strongly typed, then the resources can be accessed as properties. When you mistype the resource names, you get the error hint immediately.

How to achieve this? It is very simple. Locate the resource files, for instance, Common.resx, and then right click the file, choose Properties, and fill in Custom Tool textfield with PublicResXFileCodeGenerator. At the meantime, in the dropdownbox Build Action, choose Embedded Resource. And recompile the project.

You are ready to go.

7Aug/090

Use JavaScript to access the control Validators in ASP.NET pages

Sometimes, we want to access the Validators such as RequiredFieldValidator and CompareValidator in the client side and do something according to the user’s behavior. This can be done only use JavaScript.

Here is what I did in my application:

<script type=”text/javascript”>¨
   function void myFunction(){
      if (typeof (Page_ClientValidate) == ‘function’){
         Page_ClientValidate();
      }
      if (Page_IsValid){
         // Do something
      }
      else{
           if (!Page_Validator[0].isvalid){
              // Do something
          }
      }
   }
</script>


Tagged as: No Comments
7Aug/092

Solve the compatibility issue in IE 6, IE 7, IE 8, Firefox, Chrome, and Opera

It is really a tough job to create a site that support all the different browsers, especially for the IE 6, IE 7 and IE 8.

I, recently, encountered some design compatibility issues with different browsers. IE 8, Google Chrome, Firefox, and Opera are almost the same look if designed properly with the CSS, but for IE 6 and IE 7, the layout is totally screwed.

What I did in the Master page is to have this segment of code to avoid this compatibility issue:

<link rel="stylesheet" href="includes/css/main.css" type="text/css" />
<!--[if lte IE 6]>
   <link rel="stylesheet" href="includes/css/ie6/main.css" type="text/css" />
<![endif]-->



And then, in the includes/css/ie6/main.css file, design the layout for IE 6.

Filed under: CSS 2 Comments