'ddl' does not exist using dynamic dropdownlist control in SelectedIndexChanged

I am getting an error using dynamic dropdownlist control in SelectedIndexChanged:
lblTable.Text = "#" + ddl.SelectedItem.Tostring() + "#";//Error      The name 'ddl' does not exist in the current context in Default.aspx.cs      line:44      
Below is the complete code for clarity, Thanks in advance for any help given.
public partial class modules_create_cams_managers_Default : System.Web.UI.Page
{
	protected void Page_Init(object sender, EventArgs e)
    {
        if (Page.User.Identity.IsAuthenticated)
        {
            Page.ViewStateUserKey = Page.Session.SessionID;
        }
        InitializeComponent();
		BuildDynamicDropDown();
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
private void InitializeComponent()
    {
    }
protected void BuildDynamicDropDown()
        {
            DropDownList ddl = new DropDownList();
            ddl.ID = "newDDL";
            ddl.SelectedIndexChanged += dynamicDDL_SelectedIndexChanged;
            ddl.Items.Add(new ListItem("test1", "test1"));
            ddl.Items.Add(new ListItem("test2", "test2"));
            ddl.Items.Add(new ListItem("test3", "test3"));
            ddl.AutoPostBack = true;
            Panel1.Controls.Add(ddl);
        }// End BuildDynamicDropDown
        protected void dynamicDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
         try
		{
			string strINeedABreakPoint = string.Empty;
            //Response.Write("YES");
            lblTable.Text = "#" + "lblTable" + "#";
            lblTable.Text = "#" + ddl.SelectedItem.Tostring() + "#";//Error	The name 'ddl' does not exist in the current context in Default.aspx.cs	line:44		
        }
		catch (Exception ex)
        {
        errorLabel4.Text = "<font color='red'>" + "err:48E:PL:" + "</font>" + ex.ToString();
        } // End catch
		}// End dynamicDDL_SelectedIndexChanged
}// End Page_Load

Open in new window

homeshopperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kamal KhaleefaInformation Security SpecialistCommented:
ddl.SelectedItem.Tostring()

should be

ddl.SelectedItem.Text.Tostring()
0
homeshopperAuthor Commented:
Thankyou for your suggestion, but still get compile error:
Error      6      The name 'ddl' does not exist in the current context      Line: 44      35
0
käµfm³d 👽Commented:
You need to widen the scope of your ddl variable. You declared it within the BuildDynamicDropDown; dynamicDDL_SelectedIndexChanged cannot see this variable because it is scoped to the BuildDynamicDropDown method. Simply adding the object to the Panel's Controls collection does not mean that the variable itself can be seen within other methods of your class. Make the variable have a class scope.

e.g.

public class YourClassName
{
    DropDownList ddl;

    ...

    protected void BuildDynamicDropDown()
    {
        this.ddl = new DropDownList();

...

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
homeshopperAuthor Commented:
Thank you for your good explanation and example.
brilliant! I'll award the points.
I have put the full working code below for others to see:

aspx page:
<!-- Start Page Content -->
<table id="table1" 
style="position:absolute; left:10px; top:40px; width:750px; font-size:x-small;border:1px solid #722F90;">
<tr>
<td >
<h4>lblTable:&nbsp;<asp:Label ID="lblTable" runat="server" ></asp:Label>&nbsp;</h4>
<p>
<asp:Panel runat="server" ID="Panel1" >
        
</asp:Panel>  
</p>    
 </td>
 </tr>
 <tr>
 <td>
 <h4>errorLabel4:&nbsp;<asp:Label ID="errorLabel4" runat="server" ></asp:Label>&nbsp;</h4>
 </td>
 </tr>
 </table>
<!-- End Page Content -->

Open in new window

code-behind:
public partial class YourClassName : System.Web.UI.Page
{
	DropDownList ddl;
	protected void Page_Init(object sender, EventArgs e)
    {
        if (Page.User.Identity.IsAuthenticated)
        {
            Page.ViewStateUserKey = Page.Session.SessionID;
        }
        InitializeComponent();
		BuildDynamicDropDown();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    }
private void InitializeComponent()
    {
    }
protected void BuildDynamicDropDown()
        {
            //DropDownList ddl = new DropDownList();
			this.ddl = new DropDownList();
            ddl.ID = "newDDL";
            ddl.SelectedIndexChanged += dynamicDDL_SelectedIndexChanged;
            ddl.Items.Add(new ListItem("test1", "test1"));
            ddl.Items.Add(new ListItem("test2", "test2"));
            ddl.Items.Add(new ListItem("test3", "test3"));
            ddl.AutoPostBack = true;
            Panel1.Controls.Add(ddl);
        }// End BuildDynamicDropDown
        protected void dynamicDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
         try
		{
			string strINeedABreakPoint = string.Empty;
            lblTable.Text = "#" + "lblTable" + "#";
            lblTable.Text = "#" +  ddl.SelectedItem.ToString() + "#";
		catch (Exception ex)
        {
        errorLabel4.Text = "<font color='red'>" + "err:48E:PL:" + "</font>" + ex.ToString();
        } // End catch
		}// End dynamicDDL_SelectedIndexChanged
}// End Page_Load

Open in new window

0
homeshopperAuthor Commented:
Thank you for your good explanation and example.
brilliant!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.