How can I get the selected item of a dropdownlist, the first time my page loads?
77,758
Solution 1
There is a DataBound event
, which fires after the data is bound to the dropdown. As you are assigning the dataSource to your dropdown you need selected item after all the rows binded to dropdown
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
DropDownList1.SelectedValue // store it in some variable
}
Solution 2
You can get the Selected Value like
string selected = drp.SelectedItem.Text;
Or
string selected = drp.SelectedItem.Value;
When the page is loaded the first value is shown Selected
unless you set it by specifying the SelectedIndex
or by Text/Value
Author by
Tassisto
Specialized in software developing using .NET Technologies.
Updated on February 18, 2020Comments
-
Tassisto about 4 years
I'm 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.
Thank you in advance.
Edit: I call this method at the Load-event but ddlNiveau2 remains empty. I think that ddlNiveau1.SelectedValue isn't accessed.
public void FillListNiveau2() { ddlNiveau2.Items.Clear(); foreach (var item in dBAL.GetListNiveau2(ddlNiveau1.SelectedValue)) { ddlNiveau2.Items.Add(item.ToString()); } RemoveDuplicateItems(ddlNiveau2); }
-
Tassisto about 13 yearsdoesn't make sense? I use a datasource, this fills my dropdownlist at load-event.
-
Theun Arbeider about 13 yearsAnd how are we supposed to know that if you dont give us that information before tassisto?