.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Working with Resource file in ASP.NET

Posted By:Athira Sreejith       Posted Date: February 28, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

Resource file in ASP.NET played important role when you are developing your application based on country language.

in your .aspx:

<form id="form1" runat="server">

<div>

<table>

<tr>

<td style="width: 120px">

td>

<td>

<asp:DropDownList ID="ddlLanguage" runat="server" AutoPostBack="True">

<asp:ListItem Value="Auto">Autoasp:ListItem>

<asp:ListItem Value="ar-EG">Arabicasp:ListItem>

<asp:ListItem Value="de-DE">Germanasp:ListItem>

<asp:ListItem Value="en-US">Englishasp:ListItem>

asp:DropDownList>

td>

tr>

<tr>

<td style="width: 120px">

<asp:Label ID="LegendFirstname" runat="server" Text="Firstname" meta:resourcekey="LabelResource1">

asp:Label>

td>

<td>

<asp:TextBox ID="FirstnameText" runat="server" meta:resourcekey="TextBoxResource1">asp:TextBox>

td>

tr>

<tr>

<td style="width: 120px">

<asp:Label ID="LegendLastname" runat="server" Text="Lastname:" meta:resourcekey="LabelResource2">asp:Label>

td>

<td>

<asp:TextBox ID="LastnameText" runat="server" meta:resourcekey="TextBoxResource2">asp:TextBox>

td>

tr>

<tr>

<td style="width: 120px">

<asp:Label ID="LegendBirthdate" runat="server" Text="Birthdate:" meta:resourcekey="LabelResource3">asp:Label>

td>

<td>

<asp:TextBox ID="BirthdateText" runat="server" meta:resourcekey="TextBoxResource3">asp:TextBox> 

<asp:RegularExpressionValidator ControlToValidate="BirthdateText" ErrorMessage="Invalid date!!"

ID="RegularExpressionValidator1" runat="server" ValidationExpression='<%$ Resources:ValidationResources, DateFormat %>'

meta:resourcekey="RegularExpressionValidatorResource1">asp:RegularExpressionValidator>

td>

tr>

<tr>

<td style="width: 120px">

<asp:Label ID="LegendAnnualSalary" runat="server" Text="Annual Salary:" meta:resourcekey="LabelResource4">asp:Label>

td>

<td>

<asp:TextBox ID="SalaryText" runat="server" meta:resourcekey="TextBoxResource4">asp:TextBox> 

<asp:RegularExpressionValidator ControlToValidate="SalaryText" ErrorMessage="Invalid salary!!"

ID="RegularExpressionValidator2" runat="server" ValidationExpression='<%$ Resources:ValidationResources, SalaryFormat %>'

meta:resourcekey="RegularExpressionValidatorResource2">asp:RegularExpressionValidator>

td>

tr>

table>

div>

<asp:Button ID="OkayAction" runat="server" Text="Okay" meta:resourcekey="ButtonResource1" />

<asp:Button ID="CancelAction" runat="server" Text="Cancel" meta:resourcekey="ButtonResource2" /><br />

<br />

form>

 

.cs:

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

namespace Learn

{

public partial class Default : System.Web.UI.Page

{

protected override void InitializeCulture()

{

// string culture = string.Empty;

//if (ddlLanguage.SelectedIndex > -1)

//{

// culture = ddlLanguage.SelectedValue;

//}

//else

//{

// culture = "Auto";

//}

string culture = Request.Form["ddlLanguage"];

if (string.IsNullOrEmpty(culture))

culture = "Auto";

UICulture = culture;

Culture = culture;

if (culture != "Auto")

{

System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(culture);

System.Threading.Thread.CurrentThread.CurrentCulture = ci;

System.Threading.Thread.CurrentThread.CurrentUICulture = ci;

}

base.InitializeCulture();

}

protected void Page_Load(object sender, EventArgs e)

{

}

}

}



     

Further Readings:

Responses
Author: Suresh         Company URL: http://www.dotnetspark.com
Posted Date: March 04, 2009

hi Athira Appukuttan

when im using ur code. im getting this error. can u plz explain about this. what should i do. Im beginner of the asp.net.

Error 1 The resource object with key 'DateFormat' was not found.
Error 2 The resource object with key 'SalaryFormat' was not found

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend