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
207 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
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:mwmiller78
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

932 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

13 Experts available now in Live!

Get 1:1 Help Now