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);
}
The only possible way is to place the DropDownList inside ASP.Net AJAX UpdatePanel so that, instead of Full PostBack which causes Page refresh (reload), a Partial PostBack will occur. The HTML Markup consists of an ASP.Net ScriptManager and a DropDownList placed inside AJAX UpdatePanel.
Select the <select> element using JQuery selector. This selector is more specific and selecting the first element using option:nth-child(1).
Method 1: Using the value property: The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.
The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.
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
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With