Get Dropdown list value - set label text

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 136
Reputation: cVz is an unknown quantity at this point 
Solved Threads: 7
cVz's Avatar
cVz cVz is offline Offline
Junior Poster

Get Dropdown list value - set label text

 
0
  #1
Jun 12th, 2009
Hi everyone,

I would like to set a label's text when a dropdownlist item is selected

I want the label to display the value of the item, not the text.

Is there any way i can do so without refreshing the page i found the update panel loads far too long for my liking.

Javascript ???

i would like to do this using attributes

  1. ddBenefitPeriod.Attributes.Add("selectedIndexChanged", string.Format("{0}.innertext = {1}.options[{1}.selectedIndex].value; ", lblBenefitPeriodFactor.ClientID, ddBenefitPeriod.ClientID));

This does not work, can anyone give me some pointers please ??

Thanks in advance

Delphi & C# programmer deluxe...
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Get Dropdown list value - set label text

 
0
  #2
Jun 12th, 2009
Try this:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="daniweb.web.WebForm1" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml" >
  5. <head runat="server">
  6. <title></title>
  7. </head>
  8. <body>
  9. <form id="form1" runat="server">
  10. <div>
  11. <asp:dropdownlist ID="Dropdownlist1" runat="server">
  12. <asp:ListItem Value="Value1">Text1</asp:ListItem>
  13. <asp:ListItem Value="Value2">Text2</asp:ListItem>
  14. </asp:dropdownlist>
  15. <br />
  16. <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  17. </div>
  18. </form>
  19. </body>
  20. </html>

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace daniweb.web
  9. {
  10. public partial class WebForm1 : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. string script =
  15. string.Format("javascript:document.getElementById('{0}').innerText = {1}.options[{1}.selectedIndex].value;",
  16. Label1.ClientID,
  17. Dropdownlist1.ClientID);
  18. Dropdownlist1.Attributes.Add("onChange", script);
  19. }
  20. }
  21. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 164
Reputation: dnanetwork has a little shameless behaviour in the past 
Solved Threads: 24
dnanetwork's Avatar
dnanetwork dnanetwork is offline Offline
Junior Poster

Re: Get Dropdown list value - set label text

 
0
  #3
Jun 27th, 2009
on dropdown selectedIndex change event

LabelName.Text = dropdownList.SelectedValue.toString();
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC