473,414 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Getting a DropDownList value to concatenate with String in a textbox

24
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.
Jan 16 '09 #1
2 9244
liawcv
33
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.
Jan 16 '09 #2
uicouic
24
Oh ok. Thanks alot liawcv!

Cheers! :)
Jan 17 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tim Zych | last post by:
I'm having problems with a dropdownlist control. I fill the control with values in the page_load event. But when I click the button the dropdownlist control gets cleared out. How do I use a...
2
by: Benedict Teoh | last post by:
I created a dropdownlist containing day, month and year field and expose a property to assign a date. When I call from a aspx page and assign the value, the new date is not displayed until a submit...
2
by: jason | last post by:
Pardon my ignorance on this. The below code works, except, when I edit a record and update the two drop downs take the first entry in the dropdownlist if not selected. I'd also like the dropdown to...
1
by: jimb | last post by:
I can get the dropdownlist into the datagrid, and I can populate it, but I can't read it. Anybody have a working example of a dropdownlist in an editable grid? Thanks. -- .....
2
by: preeti13 | last post by:
Hi guys i am here with my another probelm please help me.trying insert the value into the data base but getting the null value error .I am getting thsi error Cannot insert the value NULL into...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
0
by: Dan | last post by:
Hi, I have a detailsview with two fields: in editmode, one is a textbox and the other is a dropdownlist. i want to update both fields using the detailsview. My problem: when clicking on the...
6
by: uicouic | last post by:
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.