?
Solved

Idea needed: switching text on labels

Posted on 2009-05-14
9
Medium Priority
?
160 Views
Last Modified: 2012-05-07
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.
0
Comment
Question by:Camillia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24391520
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

0
 
LVL 7

Author Comment

by:Camillia
ID: 24391578
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.
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24391651
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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24391663
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

0
 
LVL 7

Author Comment

by:Camillia
ID: 24391698
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.
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24391708
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.
0
 
LVL 7

Author Comment

by:Camillia
ID: 24391719
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?
0
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 2000 total points
ID: 24391809
Sorry I posted my last solution in a hurry. Consider this instead:
Let's say your Countries table has these columns:
  • CountryID
  • CountryName
  • LastNameString
Use the following SQL to query Countries for binding them to DDL:
SELECT CountryID + ',' + LastNameString AS CountryCode, CountryName
And use the code below in Data Binding routine:

//ddl - DropDownList object name
//Data binding routine
ddl.DataSource = cmdSql.ExecuteReader();
ddl.DataTextField = "CountryName";
ddl.DataValueField = "CountryCode";
ddl.DataBind()
 
//put this code in SelectedIndexChanged event of DDL
lblLastName.Text = ddl.SelectedValue.Split(",")(1);
 
//the code below will give you CountryID if you need it any where else in code
ddl.SelectedValue.Split(",")(0)

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 24391870
thanks, will try tomorrow.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question