Link to home
Start Free TrialLog in
Avatar of doolinn
doolinnFlag for United Kingdom of Great Britain and Northern Ireland

asked on

DropDownList in FormView - Show Selected Item in a Label

I have a dropdownlist (DropDownList1) in a formview (FormView1).

How do I show the selected item in a label (Label1) outside of the formview?

At the moment I have the following code but it only shows the initial dropdown value and does not update the label when the selected item changes. I realise I somehow have to incorporate the dropdown selected item change event into the code but I’ve no idea how & where.

Thanks!


protected override void OnInit (EventArgs e)
{
FormView1.ItemCreated += new EventHandler (FormView1_ItemCreated);
base.OnInit (e);
)

protected void FormView1_ItemCreated (Object sender,EventArgs e)
{
FormViewRow  row = FormView1.Row;
DropDownList ddl = (DropDownList)row.FindControl(“DropDownList”);
Label1.Text =ddl.SelectedItem.Text;
}
Avatar of Sammy
Sammy
Flag of Canada image

try something like this
void FormView1_ItemCreated(object sender, EventArgs e)
    {
       
           DropDownList  tmpDDL = (DropDownList)FormView1.Row.FindControl("DropDownList");
            if (tmpDDL != null)
            {
               this.Label1.Text=tmpDDL.SelectedItem.Text;
             }
    }

I have tested the code but it should work

Sammy
Avatar of doolinn

ASKER


sorry but adding an if statement to my code still does not invoke the change to the label text. any more thoughts..thanks
Avatar of doolinn

ASKER

just realised I should have had autopostback = true on the dropdown...whoops.!
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial