views:

340

answers:

2

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.

A: 

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;
}
}
Burt
+2  A: 

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;
}
Justin Swartsel
how can i access other controls in this event?!
Mahdi
Very roughly speaking, you can do something along the lines of 'Control c = (ddl1.NamingContainer as ListView).FindControl("myControlId");'. This assumes they have the same naming container (like a parent). You might also need to do a google search for "FindControl recursive".
Justin Swartsel