I am looking for a solution to get the first selected item in a DropDownList. And I want to get it when the page loads for the first time.
Thanks in advance.
Edit: I call this method in Load-event, but ddlNiveau2 remains empty. I think ddlNiveau1.SelectedValue is not available.
public void FillListNiveau2() { ddlNiveau2.Items.Clear(); foreach (var item in dBAL.GetListNiveau2(ddlNiveau1.SelectedValue)) { ddlNiveau2.Items.Add(item.ToString()); } RemoveDuplicateItems(ddlNiveau2); }
There is DataBound eventone that starts after the data is tied to a drop-down list. When you assign a dataSource to a drop-down list, you need to select the item after all the rows bound to the drop-down menu
DataBound event
protected void DropDownList1_DataBound(object sender, EventArgs e) { DropDownList1.SelectedValue // store it in some variable }
You can get the selected value, for example
string selected = drp.SelectedItem.Text;
Or
string selected = drp.SelectedItem.Value;
, Selected, , SelectedIndex Text/Value
Selected
SelectedIndex
Text/Value
Page_Load:
Page_Load
if (!Page.IsPostBack) { // Load list items .. dropDownList.SelectedIndex = 0; }
. DropDownList.
, , dropdown. SelectedValue. , , ,