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

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

:)

Try this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="daniweb.web.WebForm1" %>
<!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:dropdownlist ID="Dropdownlist1" runat="server">
				<asp:ListItem Value="Value1">Text1</asp:ListItem>
				<asp:ListItem Value="Value2">Text2</asp:ListItem>
			</asp:dropdownlist>
			<br />
    	<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace daniweb.web
{
  public partial class WebForm1 : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      string script =
        string.Format("javascript:document.getElementById('{0}').innerText = {1}.options[{1}.selectedIndex].value;", 
        Label1.ClientID,
        Dropdownlist1.ClientID);
      Dropdownlist1.Attributes.Add("onChange", script);
    }
  }
}

on dropdown selectedIndex change event

LabelName.Text = dropdownList.SelectedValue.toString();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.