Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropdownlist selected value at Selectedindexchanged event

I'm working on asp.net website with Vb.net and I have a dropdownlist with autopostback = true and I need to get the selected value when I change the item or I want to get the item which fires the selectedindexchanged event ..

any help please..

like image 254
Amr Elnashar Avatar asked Feb 21 '12 02:02

Amr Elnashar


People also ask

How to use DropDownList SelectedIndexChanged in asp net c#?

The SelectedIndexChanged event of ASP.Net DropDownList When an item is changed in ASP.Net DropDownList, the following OnSelectedIndexChanged event handler is executed. Inside the event handler, the selected Text and Value of the DropDownList Selected Item is fetched and displayed using JavaScript Alert message box.

How Show dropdown list with selected value from database in asp net?

You have to use SelectedValue property of DropDownList . DataTextField and DataValueField are for specifying which properties from DataSource should be used as Text and Value of drop down list. Save this answer.


2 Answers

In ie. your Page_Load set

this.ComboBox1.SelectedIndexChanged += new System.EventHandler(ComboBox1_SelectedIndexChanged);

Then write the event handler like this:

private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
  ComboBox comboBox = (ComboBox) sender;
  string selected = (string) comboBox.SelectedItem;
}

Make sure that in your Page_Load you write this before setting the combobox default value or you will end up with this always being the selected item:

if (Page.IsPostBack)
  return;
like image 110
Stian Avatar answered Oct 14 '22 00:10

Stian


try this:

    protected void list_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList list = (DropDownList)sender;
        string value = (string)list.SelectedValue;
    }
like image 30
Frank Lee Avatar answered Oct 13 '22 22:10

Frank Lee