943,769 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 13073
  • ASP.NET RSS
Jun 12th, 2009
0

Get Dropdown list value - set label text

Expand Post »
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

C# Syntax (Toggle Plain Text)
  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

cVz
Reputation Points: 29
Solved Threads: 7
Junior Poster
cVz is offline Offline
139 posts
since Mar 2008
Jun 12th, 2009
0

Re: Get Dropdown list value - set label text

Try this:

ASP.NET Syntax (Toggle Plain Text)
  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>

c# Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jun 27th, 2009
0

Re: Get Dropdown list value - set label text

on dropdown selectedIndex change event

LabelName.Text = dropdownList.SelectedValue.toString();
Reputation Points: 28
Solved Threads: 106
Banned
dnanetwork is offline Offline
633 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: how can i get the system admin user id and password into my asp using vb.net
Next Thread in ASP.NET Forum Timeline: Delete the file from server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC