-
August 18th, 2011, 12:28 PM
#1
Out of context Problem
Hello,
I seem to have a lack of understanding here and need some help.
When I try and compile this in MS Visual Web Developer 2010 I am getting this error
Error 5 The name 'DropDown1' does not exist in the current context
I do not understand why this happening.
Could someone please explain why this would be happening.
Thanks
Mike
if (!LabelIsDone)
{
DropDownListText = "";
LabelIsDone = true;
CtrlNbr++;
DropDownListId = "List1" ;
DropDownList DropDown1 = new DropDownList();
DropDown1.ID = DropDownListId; DropDown1.Width = 255;
DropDown1.Items.Add("TEST ENTRY");
this.PlaceHolder1.Controls.Add(DropDown1);
else
{
DropDown1.Items.Add("ENTRY 2);
}
-
August 18th, 2011, 12:44 PM
#2
Re: Out of context Problem
Greetings Mike,
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!
Code:
if (!LabelIsDone)
{
DropDownListText = "";
LabelIsDone = true;
CtrlNbr++;
DropDownListId = "List1" ;
DropDownList DropDown1 = new DropDownList();
DropDown1.ID = DropDownListId; DropDown1.Width = 255;
DropDown1.Items.Add("TEST ENTRY");
this.PlaceHolder1.Controls.Add(DropDown1);
else
{
DropDown1.Items.Add("ENTRY 2);
}
-
August 18th, 2011, 12:49 PM
#3
Re: Out of context Problem
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.
Edit fcronin beat me to it
Always use [code][/code] tags when posting code.
-
August 18th, 2011, 01:51 PM
#4
Re: Out of context Problem
Hello,
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
-
August 18th, 2011, 02:09 PM
#5
Re: Out of context Problem
Originally Posted by mfbrowne
Hello,
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 );
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|