Solved

Populating an ASP.NET label with a selection made from a ListBox using a client script. (JavaScript, etc.)

Posted on 2014-11-06
6
220 Views
Last Modified: 2014-11-06
I have a label called Label1 and a Listbox called ListBox1 on an ASP.NET web form.
I am populating Listbox1 with 4 options on the page load event in my code behind.
When the selection in Listbox1 changes, I want this change to be reflected in Label1 by assigning the text value of Label1 to the selection.
I want this action to be performed on the client side only.

How?
TIA
0
Comment
Question by:Mike Miller
[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
  • 3
  • 3
6 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40427676
with firefox, do a right on your Listbox1, choose inspect element and post the HTML for this listbox1 here.
else just post the whole body part.
0
 

Author Comment

by:Mike Miller
ID: 40427682
How about I just post my code so far...

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="test2.aspx.vb" Inherits="Diamond_Group.test2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="blahblah"></asp:Label>
        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
        <br />
    </div>
    </form>
</body>
</html>

Open in new window


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            ListBox1.Items.Add("option 1")
            ListBox1.Items.Add("option 2")
            ListBox1.Items.Add("option 3")
            ListBox1.Items.Add("option 4")
            ListBox1.Items.Add("option 5")
            ListBox1.Items.Add("option 6")
        End If
    End Sub

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 40427685
hmmm my comment was clear : with firefox, do a right on your Listbox1, choose inspect element
you can use chrome too or a recent internet explorer
0
Technology Partners: 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!

 

Author Comment

by:Mike Miller
ID: 40427687
<select size="4" name="ListBox1" id="ListBox1">
      <option value="option 1">option 1</option>
      <option value="option 2">option 2</option>
      <option value="option 3">option 3</option>
      <option value="option 4">option 4</option>
      <option value="option 5">option 5</option>
      <option value="option 6">option 6</option>

</select>
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40427693
Test pages :
http://jsfiddle.net/or73pLew/1/
http://jsfiddle.net/or73pLew/3/
<script>
window.onload = function() {
    document.getElementById("ListBox1").onchange = function() {
//        document.getElementById("label1").innerHTML = this.value;
        document.getElementById("label1").innerHTML = this.options[this.selectedIndex].text;
    }
}
</script>

Open in new window

0
 

Author Closing Comment

by:Mike Miller
ID: 40427699
Perfect, thank you.
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 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