title: How to add a DropDownList programmatically in C#
body: <p>So I've got a masterpage on which a DropDownList should be displayed. Then I've got a class which should create a DropDownList. But since I've only got one page with a DDL I don't want to write inside the masterpage.aspx. So my question is if it is even possible to write all the code inside C# to create the DropDownList. Do I need someting like that?</p>
  
  <pre><code>&lt;asp:Panel ID="pnlChannel" runat="server"&gt;
  </code></pre>
  
  <p>or can I just do it like that:</p>
  
  <pre><code>Panel pnlChannel = new Panel();
  </code></pre>
  
  <p>But if I do it in the codebehind It won't display anything</p>
  
  <p>This is my code so far:</p>
  
  <pre><code> public Panel GetDropDownList()
  {
      // Create drop down list and data source
      Panel pnlChannel = new Panel();
      DropDownList ddlChannel = new DropDownList();
      ListItem limDefault = new ListItem();
      SqlDataSource sdsChannel = new SqlDataSource();
  
      // Configure data source
      sdsChannel.ConnectionString = ConfigurationManager.ConnectionStrings["SQL"].ConnectionString;
      sdsChannel.SelectCommand = "SELECT * FROM Kanal";
      sdsChannel.ID = "sdsChannel";
  
      // Configure drop down list
      ddlChannel.DataTextField = "Kanal";
      ddlChannel.DataValueField = "Kanal";
      ddlChannel.AppendDataBoundItems = true;
      ddlChannel.DataSourceID = "sdsChannel";
  
  
  
      // Configure default list item
      limDefault.Selected = true;
      limDefault.Text = "Alle";
      limDefault.Value = "-1";
  
      // Add controls to static panel in footer
      ddlChannel.Items.Add(limDefault);
      pnlChannel.Controls.Add(ddlChannel);
      pnlChannel.Controls.Add(sdsChannel);
  
      return pnlChannel;
  }
  </code></pre>
  
  <p>I don't really know what I have to do, to display it on the page.
  If I debug it and execute the page, it calls the method but it still won't show up on the page</p>