Link to home
Start Free TrialLog in
Avatar of SirReadAlot
SirReadAlot

asked on

Cannot implicitly convert type 'object' to 'System.Web.UI.WebControls.DropDownList'

hi experts,

public void SetDropDownIndex(object sender, System.EventArgs e)
            {
                  string strItem;
                  System.Web.UI.WebControls.DropDownList ed;
                  ed = sender;=========================================================> this being highlighted
                  ed.SelectedIndex = ed.Items.IndexOf(ed.Items.FindByText(strItem));
            }

hi expert,

How do I get around this

Cannot implicitly convert type 'object' to 'System.Web.UI.WebControls.DropDownList'
SOLUTION
Avatar of WinterMuteUK
WinterMuteUK
Flag of United Kingdom of Great Britain and Northern Ireland 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
ASKER CERTIFIED SOLUTION
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
Avatar of SirReadAlot
SirReadAlot

ASKER

will try all

thanks
The difference between mine and Bowmanje's solution is that in my case the 'ed' var will be set to 'null' if the Sender isn't a DropDownList, but it won't complain, with Bowmanje's you will get an Exception thrown if sender isn't a DropDownList.

So in my version you should do:

  ed = sender as DropDownList;
  if(ed == null)
  { /* ERROR */ }

and you should wrap Bowmanje's in a try/catch:

  try{
    ed = (DropDownList) sender;
    }
   catch(InvalidCastException)
   {
      /* ERROR */
   }

Wint.
thanks guys