Friday, January 16, 2009

Resource file usage in .Net

This article describes how we can consume values from Resource file in various scenarios.

1) Consume resource value in C# code :



btnCancel.Text = Resources.myResource.ButtonCancel;




2) Consume resource value in HTML / Client control in HTML code :


<input type="button" id="btnAdd" text="<%= Resources.myResource.ButtonAdd %>" />




3) Consume resource value in Server control in HTML code :


<asp:ListItem Text="<%$ Resources:myResource, Yes%>"></asp:ListItem>



4) Consume resource value to set some para / span text in HTML code :


<p>
<asp:Literal runat="server" Text="<%$ Resources:myResource, Success%>" />
</p>



Note : In the above code, myResource is resource file name.

What I get the conclusion by implementing above scenarions are that :
In case of C# code, resource value usage is straight, just Resources.resourceFileName.keyName.
Whereas in case of HTML / ASP.Net code, if I am using HTML / Client control then we have to use "<%= ... %>" (percentage symbol) and in case of Server control we have use "<$= ... %>" (dollar symbol).

3 comments:

nagarajan said...

YOur code is working for Server controls. Not working fo HTML client side controls

nagarajan said...

Any suggestions ???

hyma said...

Perfect Post........
Thanks a lot

Google