Connecting Tech Pros Worldwide Forums | Help | Site Map

Getting a DropDownList value to concatenate with String in a textbox

Newbie
 
Join Date: Jan 2009
Posts: 23
#1: Jan 16 '09
Hi all. I need help about this particular aspect.

In a webform I have an <asp:textbox> (txtName) and <asp:DropDownList> (lstSchool) with 2 values inside.

I have typed in a value for the textbox. When I select an option from the dropdownlist, I would like that value(String) to be concatenated with the textbox's value. How do I achieve this in JavaScript?
(Microsoft Visual Studio 2005)

Any help/advice appreciated. Thanks.

Newbie
 
Join Date: Jan 2009
Posts: 23
#2: Jan 16 '09

re: Getting a DropDownList value to concatenate with String in a textbox


Let your TextBox's ID is "TextBox1", the following JS retrieve the text of the TextBox:

Expand|Select|Wrap|Line Numbers
  1. // tested with IE7 and FF3 only
  2. var txt = document.getElementById("TextBox1");
  3. var s1 = txt.value;
Let your DropDownList's ID is "DropDownList1", the following JS retrieve the selected item's value:

Expand|Select|Wrap|Line Numbers
  1. // tested with IE7 and FF3 only
  2. var ddl = document.getElementById("DropDownList1");
  3. var s2 = lst.value;
Somehow, that's no direct access to the selected item's text. Or, you can use the following JS to retrieve the selected item, then further down to its value / text:

Expand|Select|Wrap|Line Numbers
  1. // tested with IE7 and FF3 only
  2. var ddl = document.getElementById("DropDownList1");
  3. var i = ddl.selectedIndex;
  4. var s3 = ddl.options[i].value;
  5. var s4 = ddl.options[i].text;
When you combine the JS codes and make them work with your ASP.NET page, you should be careful since the Control ID is not promised to be same with the Client ID (the coming ASP.NET 4.0 allows us to fix the Client ID, luckily).

Let say: I have a TextBox named "TextBox1" and a DropDownList named "DropDownList1". I would like to have the TextBox's text and the DropDownList's selected item's text and value to be concatenated and being shown through a JS alert box when a Button named "Button1" is clicked.

1. Add a JS function to the .aspx file

Expand|Select|Wrap|Line Numbers
  1. ...
  2. // Client IDs of the TextBox and DropDownList are passed as parameters
  3. // Use meaningful variable names for your case
  4. function showResult(txtID, ddlID)
  5. {
  6.    var txt = document.getElementById(txtID);
  7.    var ddl = document.getElementById(ddlID);
  8.    var i = ddl.selectedIndex;
  9.  
  10.    var s1 = txt.value;
  11.    var s2 = ddl.options[i].text;
  12.    var s3 = ddl.options[i].value;  
  13.  
  14.    var result = s1 + " " + s2 + " " + s3;
  15.    alert(result);
  16. }
  17. ...
2. Attach the JS function call the the Button is .cs file

Expand|Select|Wrap|Line Numbers
  1. ...
  2. protected void Page_Load(object sender, EventArgs e)
  3. {
  4.    if (!Page.IsPostBack)
  5.    {
  6.       Button1.Attributes["onclick"] = string.Format(
  7.          "showResult('{0}', '{1}'); return false;", TextBox1.ClientID, DropDownList1.ClientID);
  8.       // Or set its OnClientClick property
  9.    }
  10. }
  11. ...
The following C# codes is equivalent if you don't like to use string.Format():

Expand|Select|Wrap|Line Numbers
  1. ...
  2. Button1.Attributes["onclick"] =
  3.    "showResult('" + TextBox1.ClientID + "', '" + DropDownList1.ClientID + "'); return false;";
  4. ...
Different scenarios, different solutions. Hope this can give you some ideas.
Newbie
 
Join Date: Jan 2009
Posts: 23
#3: Jan 17 '09

re: Getting a DropDownList value to concatenate with String in a textbox


Oh ok. Thanks alot liawcv!

Cheers! :)
Reply

Tags
concatenate, dropdownlist, javascript, string, textbox