我如何在 ASP.Net 网络表单中执行 For 以在每次迭代中添加带有 <asp> 标记的元素?

How can I do a For in ASP.Net Webforms that adds elements with the <asp> tag in each iteration?

在我的 .aspx 文件中,我试图通过包含 For 的脚本在下拉列表中添加列表项,但我不知道如何正确添加这些项。 我做了一个测试例子来简化问题,数字从1到10,所以你可以更好地理解我遇到的问题

我试过了

<asp:DropDownList ID="numbers" runat="server">
    <script runat="server">
        void loop() {
            for(int i=1; i<1=10; i++) {
                <asp:ListItem>i</asp:ListItem>
            }
        }
    </script>
</asp:DropDownList>

给出的错误说 "the name asp does not exist in the current context"

将这段代码放在你后面的代码中:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for(int i=1; i<10; i++) {
                numbers.Items.Add(new ListItem(i.ToString()));
            }
        }
    }