Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

Idea needed: switching text on labels

I have a user control with some textboxes and lables. I have a a dropdownlist with countries in it. Now, in US, "last name" is used. In UK, "surname' is used.

When user changes ddl from US to UK or to any other country, I want the text on label to change from "Last Name" to "Surname".

Right now, I have the label hardcoded to Last Name. Any ideas on how to do this? It's not US and UK. I will have more countries.  I thought about XML but not sure.
Avatar of Muhammad Ousama Ghazali
Muhammad Ousama Ghazali
Flag of Saudi Arabia image

You have to employ some JavaScript code if you wish to have the required fucntionality without posting back. However, for server-side you can very easily acheive it.
Set the AutoPostBack property of your DropDownList control to True. Then use the sample code below in its SelectedIndexChanged event.

switch (DropDownList1.SelectedValue)
{
  case "US":
  case "Canada":
	  lblLastName.Text = "Last Name";	  
	  break;
	  
  case "UK":
  case "Asia":
      lblLastName.Text = "Surname";
      break;
      
  default:
  	lblLastName.Text = "Last Name";
  	break;
}

Open in new window

Avatar of Camillia

ASKER

No, this is good solution if I only had , for example 2 countries. As I posted, I might have 20 countries, migth have 50 countries. This is not an efficient, elegant solution.
Have two lists of the countries in either database or web.config file in two fields/custom configuration elements. Try the code snippet below:
//Create these Values in web.config if you wish to use web.config instead of database fields
<appSettings>
	<add key="LastNameCountries" value="US,Canada,Mexico"/>
	<add key="SurnameCountries" value="UK,Japan,Pakistan,India"/>
</appSettings>
 
//put this line on top of page
using System.Configuration.ConfigurationManager;
 
string strLastNameCountries = AppSettings("LastNameCountries");
string strSurnameCountries = AppSettings("SurnameCountries");
 
//make sure SelectedValue data type of values match with the values in appsettings
string strSelectedCountry = DropDownList1.SelectedValue; 
 
if (strLastNameCountries.IndexOf(strSelectedCountry))
{        
  lblLastName.Text = "Last Name";
}
else if (strSurnameCountries.IndexOf(strSelectedCountry))
{       
  lblLastName.Text = "Surame";
}
else
{
  lblLastName.Text = "Last Name";
}

Open in new window

There was a mistake in last part of the above code. Please use the following:
if (strLastNameCountries.IndexOf(strSelectedCountry) >= 0)
{        
  lblLastName.Text = "Last Name";
}
else if (strSurnameCountries.IndexOf(strSelectedCountry) >= 0)
{       
  lblLastName.Text = "Surame";
}
else
{
  lblLastName.Text = "Last Name";
}

Open in new window

I might have 50 countries. This is an OK solution but will get hairy when I add more countries.

I'm looking for a class or something. I send it the country and it gives me back a list of labels.
So, in the class...maybe have something your code, I call it with "US" for example and then i get back a list of labels. I parse it in aspx page and assign to corresponding labels.
You can populate the list of countries with appropriate last/sur name from the database on the start of each request and place them in Session. This would also allow you to add more countries easily in your list and provide dynamic and accurate display in future.
Hope this helps.
You mean have the labels in the database and read the labels from there, keep in session?

So with this solution, the lables will be in DB?
ASKER CERTIFIED SOLUTION
Avatar of Muhammad Ousama Ghazali
Muhammad Ousama Ghazali
Flag of Saudi Arabia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks, will try tomorrow.