Mahdi
Mahdi

Reputation: 175

Get the selected value of a dropdownlist located in a listview

how can i get the selected value from a dropdownlist which is in a listview , from the DropDownList_SelectedIndexChanged event? i have always had problem with finding controls in the page :-)

    foreach (ListViewItem item in CouncilListView.Items)
    {
        CouncilIdLabel = (Label)item.FindControl("CouncilIdLabel");
    }

it just pass all the items and i don't know how to get out of the foreach when reach the wanted control.

Upvotes: 0

Views: 2637

Answers (2)

Justin Swartsel
Justin Swartsel

Reputation: 3431

If you are registering the event from within the template markup of your listview like so:

<asp:DropDownList runat='server' id='ddl1' OnSelectedIndexChange='dropdownlist_selectedindexchange' />

then all you have to do is this:

protected void dropdownlist_selectedindexchange(Object sender, EventArgs e){
    DropDownList ddl1 = (sender as DropDownList);
    String value = ddl1.SelectedValue;
}

Upvotes: 4

Burt
Burt

Reputation: 7758

You can Exit the foreach loop:

string look_for = "bbb";
ArrayList names = new ArrayList();
names.Add("aaa");
names.Add("bbb");
names.Add("ccc");

foreach (string name in names)
{
if (look_for == name)
{
break;
}
}

Upvotes: 0

Related Questions