C# 从后面的代码生成 DropDownList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/994580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:07:12  来源:igfitidea点击:

Generate DropDownList from code behind

c#asp.net

提问by Marklar

Hi I am generating a DropDownList in my code behind file

嗨,我正在我的代码隐藏文件中生成一个 DropDownList

    protected DropDownList CountryList() 
    {
        DropDownList ddl = new DropDownList();

        XDocument xmlDoc = XDocument.Load(Server.MapPath("Countries.xml"));
        var countries = from country in xmlDoc.Descendants("Country")
                        select new
                        {
                          Name = country.Element("name").Value,                              
                        };

        foreach (var c in countries)
        {
            ddl.Items.Add(c.Name);                
        }
        return ddl;
    }

I had dreams on then having <%= CountryList() %> on my aspx page. But when I do this it prints the string - "System.Web.UI.WebControls.DropDownList".

我曾梦想在我的 aspx 页面上有 <%= CountryList() %> 。但是当我这样做时,它会打印字符串 - “System.Web.UI.WebControls.DropDownList”。

Can I make this way of doing it work or do I have to setup a ContentPlaceHolder and then add the DropDownList to the content?

我可以让这种方式工作还是必须设置 ContentPlaceHolder 然后将 DropDownList 添加到内容中?

Cheers

干杯

采纳答案by CMS

The <%= %>is only a shorthand to the Response.Write method, you should add the controls programatically

<%= %>只是 Response.Write 方法的简写,您应该以编程方式添加控件

Or just add an asp:DropDownList tag there you want it, and then in the code behind, you can bind the data directly from your Linq to XML query, using the DataSource property, and the DataBind() method.

或者,只需在需要的地方添加一个 asp:DropDownList 标记,然后在后面的代码中,您就可以使用 DataSource 属性和 DataBind() 方法将数据直接从 Linq 绑定到 XML 查询。

For example:

例如:

On your .aspx file:

在您的 .aspx 文件中:

<asp:DropDownList ID="CountryListDropDown" runat="server">
</asp:DropDownList>

On your code-behind Page_Load:

在您的代码隐藏 Page_Load 上:

CountryListDropDown.DataSource = countries; // your query
CountryListDropDown.DataBind();

Since your query has only one selected field, you don't have to specify the DataValueField and DataTextField values.

由于您的查询只有一个选定字段,因此您不必指定 DataValueField 和 DataTextField 值。

回答by David McEwing

The <%=...%>tags are preprocessed by the ASP.NET page to mean <% Response.Write(...) %>therefore your approach isn't going to work and you will need a ContentPlaceHolder, Panel, PlaceHolder or other naming container to add the DropDownList to.

<%=...%>标签由ASP.NET页面预处理的意思<% Response.Write(...) %>,因此你的做法是行不通的,你将需要的ContentPlaceHolder,面板,占位符或其他命名容器DropDownList的增加。

Also if you want to have the page postbacks etc working as well you will need to create (and possibly populate) the DDL on the Page Init event and give it an ID otherwise you may end up with inconsistent view state.

此外,如果您希望页面回发等也能正常工作,您将需要在 Page Init 事件上创建(并可能填充)DDL 并为其指定 ID,否则最终可能会出现不一致的视图状态。

回答by AndyMcKenna

Declare the DropDownList like normal in your aspx page and then add the items in your Page_Load() or wherever else you are doing data binding.

在 aspx 页面中像往常一样声明 DropDownList,然后在 Page_Load() 或其他任何进行数据绑定的地方添加项目。

回答by mike

DropDownList ddl = new DropDownList();
ddl.ID = "test";
form1.Controls.Add(ddl); //your formID