Solved

How to get selected listbox item and copy it to a text box control using Java Script.

Posted on 2011-03-08
4
584 Views
Last Modified: 2012-05-11
I have an Asp.net listbox on a web form. Below that is a text box control. When I select an item from the listbox, I would like Java Script to take the selected text in the listbox and then copy it into the text box control. Can someone show me how to do this?

I am using Asp.net with C#, however I need to use Java Script to prevent postback flash. Ajax is not an option right now. Only Java Script. Can someone help me out?
0
Comment
Question by:brgdotnet
[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
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:nbove
ID: 35072760
Just an FYI, this really isn't an ASP.NET question, it is a javascript question and you would probably get better results if you posted in the correct area.  That being said, I think you can accomplish what you want with the following code:

<asp:ListBox ID="MyListBox" runat="server" onchange="ListBoxChanged(this);">
<asp:ListItem Text="1" Value="1" />
<asp:ListItem Text="2" Value="2" />
</asp:ListBox>
<asp:TextBox ID="MyTextBox" runat="server">
<script type="text/javascript">
  function ListBoxChanged(listBox) {
    document.getElementById('<%= MyTextBox.ClientID %>').value = listBox.items[listBox.selectedIndex].text;
  }
</script>
0
 
LVL 2

Author Comment

by:brgdotnet
ID: 35074439
Hi nbove. I can't get your code to work. Your code does not work.

I have been working with various Java Script and can't get it to work with Asp.net listboxes. I think with Asp.net the listbox working with Java Script is different then just your typical listbox control. So I think the question should remain in Asp.net since the Java Script needs to work with the listbox control.
0
 
LVL 7

Accepted Solution

by:
nbove earned 260 total points
ID: 35075093
I tested and the code works.  Give me a bit more information other than "Your code does not work." and I might be able to help you.
0
 
LVL 2

Author Closing Comment

by:brgdotnet
ID: 35137577
Thanks Champ!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

687 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