简体   繁体   中英

Trouble binding C# list to DropDownList in ASP.net

I've been banging my head against the wall for the past hour or so because I can't figure out what I'm doing wrong in this seemingly straight-forward process.

Here is what the ASPX page looks like:

<%@ Page Title="Teams" Language="C#" AutoEventWireup="true" CodeBehind="TeamEntry.aspx.cs" Inherits="Team.Model" Runat="server" Debug="true"%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
        <asp:DropDownList
            runat="server" 
            ID="DDL_Teams" 
            Width="183px">
        </asp:DropDownList>
        <input id="Text1" type="text" /><input id="Submit1" type="submit" value="submit" />
    </form>
</body>
</html>

and here is the code behind:

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using Team;

namespace Team
{
    public partial class TeamEntry : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                using (var DDL_Teams = new DropDownList())
                {
                    DDL_Teams.DataSource = TeamsList;
                    DDL_Teams.DataBind();
                }
            }
        }

        List<string> TeamsList = new List<string>()
        {
            "Alpha", "Bravo", "Charlie", "Delta"
        };
    }
}

...but all I see when I try to run the page is an empty dropdown list

I've tried several other approaches noted in other StackOverflow questions related to data binding to dropdown lists (for example, the ones listed on this page ) to no avail. Any help would be much appreciated.

You are creating a new dropdown list every time as per this code

using (var DDL_Teams = new DropDownList())

That's why no binding has occured.

But need to use the dropdown list ID created in HTML. Please use this code in TeamEntry.aspx.cs

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                DDL_Teams.DataSource = TeamsList;
                DDL_Teams.DataBind();

            }


        }
        List<string> TeamsList = new List<string>()
        {
            "Alpha", "Bravo", "Charlie", "Delta"
        };

Remove your Dropdownlist on your server side code:

if (!IsPostBack) 
        {
            //using (var DDL_Teams = new DropDownList()) - Comment this line
            {
                DDL_Teams.DataSource = TeamsList;
                DDL_Teams.DataBind();
            }
        }

You already have DDL_Teams on your web form already. Try to clean your solution and rebuild it.

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