简体   繁体   中英

How to add a DropDownList programmatically in C#

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?

<asp:Panel ID="pnlChannel" runat="server">

or can I just do it like that:

Panel pnlChannel = new Panel();

But if I do it in the codebehind It won't display anything

This is my code so far:

 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;
}

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

Html Markup:

Add a panel control on your .aspx page

<asp:Panel ID="Panel1" runat="server">
</asp:Panel>

Code-behind: on pageload

DropDownList ddl = new DropDownList();
ddl.DataSource = sdsChannel;
ddl.DataTextField = "Kanal";
ddl.DataValueField = "Kanal";
ddl.DataBind();
Panel1.Controls.Add(ddl);

Html Markup :

Added panel control on master page under form tag

<asp:Panel id="pnlMasterPage" runat="server">
</asp:Panel>

Method 1 : On masterpage on_load

DropDownList ddl = new DropDownList();
ddl.DataSource = sdsChannel;
ddl.DataTextField = "Kanal";
ddl.DataValueField = "Kanal";
ddl.DataBind();
pnlMasterPage.Controls.Add(ddl);

Method 2 : If you want to bind from child page then, on child page on_load

DropDownList ddl = new DropDownList();
ddl.DataSource = sdsChannel;
ddl.DataTextField = "Kanal";
ddl.DataValueField = "Kanal";
ddl.DataBind();
Panel pnl = this.Master.FindControl("pnlMasterPage") as Panel;
pnl.Controls.Add(ddl);

My experience is based on WinForms with C#, still it may be relevant.

As in your code, this could work:

Panel pnlChannel = GetDropDownList(); //returns a configured panel with controls.
pnlChannel.Location = new Point(56,72); //Should not be required
pnlChannel.Size = new Size(264, 152); //Should not be required
pnlChannel.Visible=true; //(Or sometimes Show(), depending).

Do you know if the panel has to be added somewhere? Sorry, my exp is based on WinForms, it's not required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM