简体   繁体   English

无法将 C# 列表绑定到 ASP.net 中的 DropDownList

[英]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:这是 ASPX 页面的样子:

<%@ 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.我尝试了其他 StackOverflow 问题中提到的其他几种方法,这些问题与数据绑定到下拉列表(例如, 本页上列出的那些)有关,但无济于事。 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.但需要使用 HTML 中创建的下拉列表 ID。 Please use this code in TeamEntry.aspx.cs请在 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.您的 web 表格上已经有DDL_Teams Try to clean your solution and rebuild it.尝试清理您的解决方案并重建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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