This problem has presented itself thru several versions of Visual Studio. When it happened again I got on the message boards to see what others are doing about it. There atre several threads about this and very litle mention about it on any MS boards or forums. MS hasnt owned up to this one yet (as far as I can find...).
If you remove the code-behind references so you can run debug and look at the Locals window the DropDownList will show up under the tree item "this". However, the solution has lost all visibilty to it in code-behind.
Oh, and no matter how many successful builds I did, the error always came back. I tried building with the control in the markup and without references in the code behind, deleting it all and rebuilding, cleaning out the solution of backups, double-referenced classes, etc. All kinds of scenario's ad nauseum.
So it leads you to a workaround which is to use FindControl to get a reference to it and cast it to a local protected variable of that same type. However, you must use the id name found in the control tree and not the ID you used in the markup. Keep in mind that DropDownList1 becomes ctl00$Main$DropDownList1 at runtime. (or something like it depending upon master pages, etc....)
To get the runtime ID, set the markup directive Trace="true" to get the control tree output at the bottom of the page in question. Copy and paste the id into the FindControl argument and you have it. The trace output will look something like this:
ctl00$Main$ctl02 System.Web.UI.ResourceBasedLiteralControl 323 0 0
ctl00$Main$DropDownList1 System.Web.UI.WebControls.DropDownList 436 216 0
ctl00$Main$ctl03 System.Web.UI.LiteralControl 152 0 0
ctl00$Main$lblcourtHouseName System.Web.UI.WebControls.Label 188 0
Depending upon your comfort level with which control it is going to find, you may want to do a little checking on it first before using the casted control, so check the type and do your due-diligence.
Here's the code:
public partial class Default_aspx : System.Web.UI.Page { protected DropDownList myDropDownListFix; protected void Page_Load( object sender, EventArgs e ) { // get a reference to the desired control Control myControl = FindControl("ctl00$Main$DropDownList1"); // make sure you have the right type if (myControl.GetType() == typeof(System.Web.UI.WebControls.DropDownList)) { // now safe to cast it to the one you want and work with it myDropDownListFix = (DropDownList)FindControl("ctl00$Main$DropDownList1"); myDropDownListFix.Items.Add("Me"); myDropDownListFix.Items.Add("You"); myDropDownListFix.Items.Add("Calib"); myDropDownListFix.Items.Add("The Horses"); } } } . . . .
I'm sure there are more elegant ways to do this, but it is a *workaround*.
BTW, after I did the manual fix, it all started working again. So, I'm not sure whether to take the workaround out or not. Not real dependable there MS.....VS200x gotta love it. Definitely a love-hate relationship.
I know this doesnt address any event handler problems you may have when you do not have visibility to the control. If I wind up in visibilty purgatory again, I will post the fix here, friends.
But for now it started working. Not LOL.
Cheers,
Chuck.
