[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3708
  • Last Modified:

DROP DOWN LIST EVENTS

Drop down list events help required

i have dop down list with 8 values

say example 12 months now when a user select avlaue i want to disply that vallue in a label

i repeat dop down list value when select3ed is disolayed in a label

how do i this C# code preferabbly
0
dontnetsanjay
Asked:
dontnetsanjay
  • 3
  • 3
  • 2
1 Solution
 
Jai STech ArchCommented:
in the SELECTEDINDEXCHANGED event of the combo box write the code

labelname.text = comboxbox.selecteditem.text
0
 
Jai STech ArchCommented:
use this

labelname.text = cboname.Items[cboname.SelectedIndex].ToString()
0
 
dontnetsanjayAuthor Commented:
hey jagnesh i will send my code now
i am tring to do this but its not happening


   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = "hello";

    }
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
TimCotteeCommented:
If you want this to be a purely client-side implementation then:

MyDDL.Attributes.Add("onchange",String.Format("document.getElementById('{0}').innerHTML=this.value;",MyLabel.ClientID);

Add this in your page_load event handler. When you select an item from the drop-down the value (not the text displayed if different) will be shown in the label.
0
 
Jai STech ArchCommented:
wht happens ? is your label text not getting changed ? or what ?
0
 
dontnetsanjayAuthor Commented:
Hey tim it has worked thanks

this is he final string which i have achieved

DropDownList1.Attributes.Add("onchange",String.Format("document.getElementById('{0}').innerHTML=this.value;",Label1.ClientID));

can u answer one more querry

where can i get explanation for this string? so that i know how it has actually happened
0
 
dontnetsanjayAuthor Commented:
jagnesh thanks

i got the solution but nothing happened in that case


0
 
TimCotteeCommented:
Ok, I'll try to explain what it does.

A standard HTML element can have many attributes e.g., onchange="" onclick="" onblur="" etc to add a bit of script to the client-side events. Asp.net doesn't give you a wrapper for these attributes except for the onclientclick="" one. However you can use attributes.add method to add any attribute you like to the html element that asp.net renders its control into. So you can add the standard event handlers or even your own unique attributes if needed. In this case we are adding an onchange event handler attribute.

The second thing that you need to know is that asp.net renders LABEL controls as <span></span> elements so you have to use the .innerHTML property and not the .value property.

Last thing is that the client-side ID of the label control can be retrieved from the .ClientID property of the control (as with any control in fact).

Putting all this together we end up with a client-side event handler that fires when the dropdownlist selection changes that sets the innerHTML property of the relevant <SPAN> element to the value of the selected item in the drop down list.

This method can be used for a wide variety of situations.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now