473,472 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to tie up a TextBox and a DropDownList?

Hi there,

I am working with ASP.NET. I have a TextBox and DropDownList (for customer
info input) that I need to tie up, i.e., when the DropDownList item changes
(the customer name), the TextBox should reflect the selected value (the
customer number). And when a user types something in the TextBox and exits
the control, the DropDownList item should be changed to that value, showing
the corresponding customer name.

I am new to JS so I would appreciate code samples if possible, but any
pointer in the right direction is much appreciated as well.

Regards,

-Benton


Jul 23 '05 #1
4 2657
VK
> info input) that I need to tie up, i.e., when the DropDownList item changes
(the customer name), the TextBox should reflect the selected value (the
customer number).


If you're working with ASP.NET then it's called "binding", not "tie
up". Will help for googling in the future.
I don't see what it has to do with ASP.NET in this particular cases
(unless you're updating your server data right away).

In the common case:
(I just attached the list scroll to more conventional <Enter> key
press. Also see in this group about select onchange event and some
accessibility issues.)

<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF">
<form name="myForm">
<select name="Customers"
onchange="this.form.CustomerID.value=this[selectedIndex].value">
<option selected value="cust1234">John Doe</option>
<option value="cust5678">Michael Smith</option>
<option value="cust9012">Mary Smith</option>
</select>
<br>
<input type="text" name="CustomerID" onkeypress="
var key = event.which || event.keyCode;
if ((key)&&(key==13)) {
var foo = (event.preventDefault) ?
event.preventDefault() :
(event.returnValue = false);
with (this.form.Customers) {
for(i=0; i < options.length; i++) {
if (options[i].value == this.value) {
options[i].selected = true;
break;
}
}
}
}
">
</form>
</body>
</html>

Jul 23 '05 #2
Lee
Benton said:

Hi there,

I am working with ASP.NET. I have a TextBox and DropDownList (for customer
info input) that I need to tie up, i.e., when the DropDownList item changes
(the customer name), the TextBox should reflect the selected value (the
customer number). And when a user types something in the TextBox and exits
the control, the DropDownList item should be changed to that value, showing
the corresponding customer name.

I am new to JS so I would appreciate code samples if possible, but any
pointer in the right direction is much appreciated as well.


<html>
<head>
<title>demo</title>
<script type="text/javascript">
function setId(sel) {
if(sel.selectedIndex) {
sel.form.userId.value=sel.options[sel.selectedIndex].value;
} else {
sel.form.userId.value="";
}
}
function setName(textField) {
var sel=textField.form.userName;
for(var i=1;i<sel.options.length;i++) {
if(textField.value==sel.options[i].value) {
sel.options.selectedIndex=i;
return;
}
}
sel.options.selectedIndex=0;
alert("User ID "+textField.value+" is unknown");
}
</script>
<body>
<form>
<select name="userName" onchange="setId(this)">
<option>Choose Name...</option>
<option value="123">Angela</option>
<option value="234">Bob</option>
<option value="345">Chuck</option>
</select>
<input name="userId" onchange="setName(this)">
<br>
<input type="button" name="dummy" value="do nothing">
</form>
</body>
</html>

Jul 23 '05 #3
>> info input) that I need to tie up, i.e., when the DropDownList item
changes
(the customer name), the TextBox should reflect the selected value (the
customer number).
If you're working with ASP.NET then it's called "binding", not "tie
up". Will help for googling in the future.


Thanks for the tip. English is not my native tongue so your clarification is
helpful indeed.
In the common case:
(I just attached the list scroll to more conventional <Enter> key
press. Also see in this group about select onchange event and some
accessibility issues.)
Wow, you provided me with more than I expected. I appreciate it a lot.

Regards,

-Benton
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF">
<form name="myForm">
<select name="Customers"
onchange="this.form.CustomerID.value=this[selectedIndex].value">
<option selected value="cust1234">John Doe</option>
<option value="cust5678">Michael Smith</option>
<option value="cust9012">Mary Smith</option>
</select>
<br>
<input type="text" name="CustomerID" onkeypress="
var key = event.which || event.keyCode;
if ((key)&&(key==13)) {
var foo = (event.preventDefault) ?
event.preventDefault() :
(event.returnValue = false);
with (this.form.Customers) {
for(i=0; i < options.length; i++) {
if (options[i].value == this.value) {
options[i].selected = true;
break;
}
}
}
}
">
</form>
</body>
</html>


Jul 23 '05 #4
> <html>
<SNIP>

It works wonderful, much appreciated.

Regards,

-Benton
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: NewToDotNet | last post by:
Hi, I am very new to ASP.NET and web programming in general. I have one issue. I have a Datagrid object with Edit template. In one Datagrid row, I have 1 DropdownList, 1 textbox and 1 readonly...
0
by: Paul Rees | last post by:
I am wondering if anyone has encountered a similar problem to the one that I am having. I have an ASP.NET page which has a table and a dropdownlist that was added at design time. The majority of...
6
by: tfs | last post by:
I am trying to display some data that is about 200 characters long and I was trying to display it on the screen in a textbox <asp:textbox /> But I don't want any borders. How would I...
0
by: Murugan V via DotNetMonster.com | last post by:
Hi, i want to add the DropDownList,TextBox and Label in detailsview dynamically at runtime.At the page load, the detailsview displayed the records.when i click the edit button,these controls should...
3
by: ton | last post by:
How can I detect that the user has modified the data on the page where several textboxes, checkboxes and dropdownlist exist. Ton
2
by: Atreju | last post by:
Ok I got form within a c sharp page. Situation: On the form I have a drop downliwst and a textbox, the dropdownlist is populated with products and the textbox has a default vale of the product...
2
by: André | last post by:
Hi, I made a detailsview containing several fields for inserting only. I also made a dropdownlist which is invisible at start. Now, when clicking in a particular textbox, the dropdownlist must...
0
by: Jacob Donajkowski | last post by:
Once the user logs in I want to have the users switch from the Roster View to the Profile View and enter their profile infomation and save it. Then the next time they login and go to the Profile...
1
by: CindyH | last post by:
Hi I'm using vs.net 2003. I have a dropdownlist inside a datagrid. I would like to be able to select a value from the dropdownlist and then have a textbox which is also inside the datagrid filled...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.