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'
SirReadAlotAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
BowmanjeConnect With a Mentor Commented:
You need to explicitly cast the object.  Like this...

ed = (DropDownList) sender;

HTH,
-Josh
0
 
WinterMuteUKConnect With a Mentor Commented:
You need to do:

   ed = sender as DropDownList

then check if it's null!

Wint.
0
 
SirReadAlotAuthor Commented:
will try all

thanks
0
 
WinterMuteUKCommented:
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.
0
 
SirReadAlotAuthor Commented:
thanks guys
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.