Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

c# - 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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...