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
203 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:mwmiller78
  • 3
  • 3
6 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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:mwmiller78
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:mwmiller78
Comment Utility
<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
Comment Utility
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:mwmiller78
Comment Utility
Perfect, thank you.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now