Tuesday, October 4, 2011

Using Resource File in Asp.Net

If you want to maintain bulk of constants or Text strings in .Net technology. we can use resource file to store or maintain all your content.

So first, Add a resource file in to your solution.

Adding Resource File

1) Right click on your solution and select Add New Item.



2) In Add New Item dialogue box select Resource file.



3) Click Add
4) You will see a new Dialogue Box "You are attempting to add a special type......"



5) Click YES then Resource file is created.





After creating a New Resource file. Here you see three columns Name,Value,Comment. In the Name field you need to use a unique name in this resurce file. Where this name field is used to pull its value from any web page. In the Value field you can enter some value which is a String. you can use comment field to make your own comment.Shown in Figures above.

So lets access this from a Web Page.

In C# Code
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(global::Resources.Resource.SomeName);
}

OutPut:
I am coming from Resource File SomeValue


This way you can any number of Strings in the resource file and access them in n number of pages.

1 comment: