Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the selected item of a dropdownlist, the first time my page loads?

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);
}
like image 347
Tassisto Avatar asked May 11 '11 08:05

Tassisto


People also ask

How do I stop page refresh on selecting the dropdown list?

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.

How do I select the first element in a DropDownList using JQuery?

Select the <select> element using JQuery selector. This selector is more specific and selecting the first element using option:nth-child(1).

How do you display a selected value in a drop down list?

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.

How do I add a default item to DropDownList?

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.


2 Answers

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
}
like image 158
Muhammad Akhtar Avatar answered Oct 16 '22 23:10

Muhammad Akhtar


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

like image 40
V4Vendetta Avatar answered Oct 17 '22 00:10

V4Vendetta