Accessing Global Resource From ASP.NET Custom Controls

Posted by Ikhwan on April 16, 2008

To programmatically get global resource object from code-behind for Page or UserControl, we usually use the GetGlobalResourceObject() method, passing in the resource name and the key. You’ll be fine until you need to call it from other than a Page or a UserControl or anything that does not inherit from TemplateControl.

I that need to use it from a custom control, which inherits from CompositeControl, which has no TemplateControl anywhere in it’s inheritance tree. Although I can access it’s Page property which holds the reference to the containing Page object, I cannot do Page.GetGlobalResourceObject() since that method is marked as protected.

Through some blog posts, I’ve been left with assuming that this is a serious design flaw. Why in the world that it is not marked as public?! After trying several hacks and finding leeways to get around this, I finally find out that you can get the similar method from HttpContext.

Just a simple call to HttpContext.GetGlobalResourceObject()! It’s a static method, so you can call it anywhere. That simple!

Working with resources is tricky business, since some of the classes are generated and compiled at runtime by the ASP.NET engine. Even though Visual Studio can assist you by “pre-compiling” them and let them appear in IntelliSense, sometimes you’ll just wonder where the heck that some class or some method comes from!

My VS Settings

Posted by Ikhwan on March 14, 2008

This is how my Visual Studio currently looks like:

VS 2008-03-14

If you want to pimp your VS to look as sweet as mine, download my vssettings here.

The color is based on Dark Grey scheme from Commonality. I made it darker and swap some colors. He also got some other schemes that worth checking out.

The code snippet in screenshot above is from Jeff Atwood’s programming font code sample.

What I like about my settings:

  • Dark background, feels more relaxing to my eye.
  • Just enough contrast, no pitch black or pure white.
  • Includes my ultimate VS toolbar.
  • The window layout. Toolbox is hidden as I don’t use it much, the editor is as large as possible, solution explorer and output window as second and third most important window.
  • Subtle cue for parenthesis/bracket matching, doesn’t pop out too much when typing, yet still visible.
  • Consolas! Best font for code eva!!1! But it doesn’t look as good on white background in my monitor. For white background, Inconsolata seems to be better?
  • Easily distinguish string literals since my chubby fingers tend to typo a lot.
  • Important comments stands out, while comments that serve as explanations dimmed down. The background for XML doc is the most awesome, serves as a separator too.

Total sweetness!!!

p.s: When importing, just select the settings you want, i.e. select syntax highlight, but unselect the toolbar, the layout or other settings.

Random Awesomeness 2008-02-19

Posted by Ikhwan on February 19, 2008

Take a look at this, columnName is a bit data type, 1 or 0.

SELECT SUBSTRING('YesNo', 4 - 3 * columnName, 3) as YesNo
FROM TableName

Neat huh? Although I prefer to collect real data from database and do formatting in the client code, this is a sweet hack nevertheless. Got it from this blog entry.

— UPDATE —

This is (somewhat) the equivalent in C#.

Console.WriteLine("YesNo".Substring(3 - 3 * Convert.ToInt16(boolValue),
                                    2 + Convert.ToInt16(boolValue)));

Or… You can just do this instead :P

Console.WriteLine(boolValue ? "Yes" : "No");

C# Cascading Constructors

Posted by Ikhwan on February 11, 2008

I got this question, on whether the default constructor is being called when you instantiate an object using another of its constructor.

Using automatically implemented properties in C# 3.5, I find myself frequently needing to override the default constructor to instantiate default instances to member properties, such as for the TaskItems in this sample code below.

class Workman
{
    public string Name { get; set; }
    public string StaffNo { get; set; }
    public List<string> TaskItems { get; private set; }

    // Default constructor
    public Workman()
    {
        TaskItems = new List<string>();
    }

    // 2nd constructor
    public Workman(StaffProfile profile) : this()
    {
        this.Name = profile.Name;
        this.StaffNo = profile.StaffNo;
    }
}

After some simple test, I found that the default constructor is not being called if I instantiate the object using the second constructor,

var man = new Workman(staffProfile);

Unless if you supply this() for the second constructor. It works like base() when you want to call specific parent constructor in an inherited class.

Furthermore, you can call other constructor, not just the default. Let’s say you have a third constructor,

    // 3rd constructor
    public Workman(string name, string staffNo) : this()
    {
        this.Name = name;
        this.StaffNo = staffNo;
    }

You can change the original second constructor to this,

    // 2nd constructor
    public Workman(StaffProfile profile) : this(profile.Name, profile.StaffNo)
    {
    }

MSBuild Missing Reference Error

Posted by Ikhwan on February 06, 2008

I am exploring easier ways to compile our codes by using MSBuild which can compile solutions through command line without launching the heavy Visual Studio. I tried it in this one solution of ours, it failed with some missing reference error, where as compiling in Visual Studio is successful. I heard that Visual Studio is also using MSBuild underneath, so it’s kind of weird.

The problem is caused by “implicit reference” (not sure if it’s a standard phrase), found at MSDN forum.

That is, project A invokes a method that’s defined in project B and project B invokes a method that’s defined in project C. And while project A has an explicit reference to project B, and project B has an explicit reference to project C, there is no explicit reference from project A to project C.

Visual Studio appears to resolve the implicit reference of A=>C, but MSBUILD doesn’t.

I solved the problem by manually adding a reference from project A to project C (although if another developer later uses the “remove un-needed references” feature, that reference will disappear from project A).

Now, I can just navigate to the project folder in my command prompt, svn update if necessary, and type msbuild.

Poster .NET Framework 3.5

Posted by Ikhwan on February 03, 2008

.NET Framework 3.5 PosterKorang boleh download .NET Framework 3.5 Namespace poster kat sini (jumpa via ScottGu’s blog). Poster ni mengandungi senarai semua class yang ada dalam .NET Framework 3.5.

Antara yang menarik perhatian aku, dari ASP.NET, ada namespace System.Web.ApplicationServices yang mengandungi class AuthenticationService, ProfileService, dan RoleService.

Aku selama ni memang tak berapa gemar nak gunakan MembershipProvider sebab susah nak configure. Aku lebih suka pada configuration menggunakan Dependency Injection (aku guna Castle Windsor) berbanding menggunakan web.config. Adakah aku boleh gunakan benda-benda ni bersama DI?

Ada juga System.Web.UI.WebControls.ListView. Mungkin ia menggantikan DataList, seperti mana GridView menggantikan DataGrid?

Selain itu dari bahagian Fundementals, ada System.Collection.Generic.HashSet<T>. Apa benda ni agaknya? Ada juga namespace System.Runtime.Serialization.Json. Bagus juga, ada alternative lain untuk human-readable serialization selain XML.

Tak lupa the whole new LINQ stuff. Ini mesti tengok punyah.

I’ll be definitely checking out those stuff later.

Deploying Crystal Reports for .NET

Posted by Ikhwan on January 31, 2008

If your application uses Crystal Reports that comes with Visual Studio, you can deploy the needed DLL into the target machine by running the MSI installer located at C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bootstrapper\Packages\CrystalReports. For mine, it’s called CRRedist2005_x86.msi. For other version of Visual Studio, it might be named differently.

p.s: Feeling super down these couples of days :(