【问题标题】:How to populate dropdown list before page loads in webforms?如何在 Web 表单中加载页面之前填充下拉列表?
【发布时间】:2014-06-18 03:45:34
【问题描述】:

我的控件 (System.Web.UI.UserControl) 中有以下 Page_Load 方法:

protected void Page_Load(object sender, EventArgs e)
{
    DropDownList ShowAssumptions = new DropDownList();
    List<string> list = new List<string>()
    {
        "test",
        "test2"
    };
    ShowAssumptions.DataSource = from i in list
                                 select new ListItem()
                                 {
                                     Text = i,
                                     Value = i
                                 };
    ShowAssumptions.DataBind();
}

然后,在我的 .aspx 中有这个:

<asp:DropDownList id="ShowAssumptions" runat="server">
</asp:DropDownList>

但是,DropDownList 永远不会被填充。我做错了什么?

【问题讨论】:

    标签: c# asp.net drop-down-menu webforms


    【解决方案1】:

    只需将列表指定为数据源。另外我假设您不想在每个 PostBack 上重新加载列表。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
             List<string> list = new List<string>()
             {
                "test",
                "test2"
             };
            ShowAssumptions.DataSource = list;
            ShowAssumptions.DataBind();
        }
    }
    

    【讨论】:

    • 啊,我明白了。谢谢!看起来我的问题是DropDownList ShowAssumptions = new DropDownList();。我把它拿出来后,它就起作用了。
    • 当您已经创建了它的asp版本时,您不必创建控件。抱歉,我不知道该怎么说。
    【解决方案2】:

    如果你使用 ASP.NET WebForms、EF 和 Bootstrap 试试这个

    HTML

    <div class="form-group">    
    
    <label class="control-label" for="inputType">Lines: </label>                            
    
    <asp:DropDownList ID="DropDownListFabricLines" CssClass="dropdown form-control" runat="server"></asp:DropDownList>
    
    </div>
    

    C#

    var entities = new DababaseEntities();
    
    List<FabricLineView> fabricLines =  entities .Line.Select(x=> new FabricLineView { ID = x.LineaID, Name = x.LineaNombre }).ToList();
    
    DropDownListFabricLines.DataValueField = "ID";
    DropDownListFabricLines.DataTextField = "Name";
    DropDownListFabricLines.DataSource = fabricLines;
    DropDownListFabricLines.DataBind();
    
    
    public sealed class FabricLineView
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    

    【讨论】:

    • 使用对象列表的好例子。
    【解决方案3】:
      protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                foreach (string item in list)
                {
                    ShowAssumptions.Items.Add(item);
                }
            }
        }
    

    【讨论】:

    • 非常感谢,但它似乎仍然无法正常工作。还有什么我可以尝试的吗?
    【解决方案4】:
    protected void Page_Load(object sender, EventArgs e)
        {
            //Don't do this here!
            //DropDownList ShowAssumptions = new DropDownList();
    
            List<string> list = new List<string>()
            {
                "test",
                "test2"
            };
            this.ShowAssumptions.DataSource = from i in list
                                         select new ListItem()
                                         {
                                             Text = i,
                                             Value = i
                                         };
            this.ShowAssumptions.DataBind();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      • 2017-12-12
      • 2017-05-22
      • 1970-01-01
      • 2017-03-06
      • 2012-10-24
      • 1970-01-01
      相关资源
      最近更新 更多