First, if you put code in a post, use the [code][/code] tags around the code to make reading easier for everyone (as seen below).
So, take a look at your code as it is formatted below, and pay attention to where you are creating the "new DropDownList();". Since you are only creating it if your LabelIsDone is false, it doesn't exist in the code that gets executed if LabelIsDone is true. So when you try to reference it there, you get that message... hope that makes sense for you.
Do some reading on "scope" to learn more about where objects are available depending on how and where they are declared and created, etc.
Also, remember that { and } must match (be paired up), if you open a block of code with a {, at some point, you need to close that block of code with a } ... look just above the "else" line, you are going to need to remedy that too.
If you still need help or clarifications... feel free to ask some more... best of luck!
You have defined your dropdown1 inside the If portion of your code but then try to access it in the else portion. If you want to use it in both places then you need to define it before the If portion.
Objects are only valid within the scope they are created.
Thank you for the information. This if is actually contained withing a while loop, so the rational behind the if (!LabelIsDone) id teel me I have created the dropdownlist and now I am filling in values. I guess I need to rethink the creation of the dropdown.
The missing } was just a miss on transcribing the selected code to the board.
Well back to the code and see what I can do to rework it.
Thank you for the information. This if is actually contained withing a while loop, so the rational behind the if (!LabelIsDone) id teel me I have created the dropdownlist and now I am filling in values. I guess I need to rethink the creation of the dropdown.
The missing } was just a miss on transcribing the selected code to the board.
Well back to the code and see what I can do to rework it.
Mike
Just create the DropDownList instance once outside the while loop. After you've cycled through the while loop and added values to the list, check the dropdown list count and add it to the controls collection if the count is greater than 0.
Code:
DropDownList DropDown1 = new DropDownList();
dropDown1.ID = "List1";
dropDown1.Width = 255;
while( somecondition )
{
// add items to the dropdown as appropriate
}
if( dropDown1.Items.Count > 0 )
{
PlaceHolder1.Controls.Add( dropDown1 );
}
Bookmarks