c# - 如何根据数据库中的数字字段填充动态下拉列表?

标签 c# mysql sql asp.net

我正在尝试实现一个下拉菜单,其中包含数字 1 - 数据库中的值

我可以在下拉列表中显示数据库中的值,但无法将 1 开始的数字创建为下拉列表中的选项。

下面是我的asp代码:

    <asp:SqlDataSource
    ID="selectSprintLength"
    runat="server"
    ConnectionString="<%$ ConnectionStrings:scConnection %>"
    SelectCommand="SELECT * FROM sc_sprints WHERE scSprintID = @sprint">
    <SelectParameters>
        <asp:QueryStringParameter QueryStringField="sprint" Name="sprint" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

<asp:DropDownList ID="sprintLengthDropDown"
    runat="server"
    OnSelectedIndexChanged="sprintDropDown_SelectedIndexChanged"
    DataSourceID="selectSprintLength"
    DataTextField="scSprintTotal"
    DataValueField="scSprintTotal"
    AutoPostBack="true"
    EnableViewState="true" />

这是我的 C# 代码:

protected void sprintDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    int counter = 0;
    int x = Int32.Parse(sprintLengthDropDown.SelectedValue);
    do
    {
         counter++;
    }
    while (counter < x);
}

任何人都可以帮忙,以便值 1 - scSprintTotal 成为下拉列表中的所有选项。例如 scSprintTotal 为 7,下拉值将为 1, 2, 3, 4, 5, 6, 7。

提前致谢!

最佳答案

要使用系列值填充下拉列表,我们可以使用 Enumerable.Range(starting_number, range);请看教程https://www.dotnetperls.com/enumerable-range

sprintLengthDropDown.DataSource = Enumerable.Range(1, 7);
sprintLengthDropDown.DataBind();

您可以将范围动态设置为数据库中的值。

关于c# - 如何根据数据库中的数字字段填充动态下拉列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47554330/

相关文章:

c# - .NET 通过反射获取私有(private)属性

c# - INotifyPropertyChanged 和线程

java - 我正在尝试将 mysql 查询结果打印到 jLabel 中

PHP - 如何让搜索引擎从一个日期搜索到另一个日期

mysql - name_scope 的 PGError 语法问题

C#:调整图片框大小以适合图像

c# - 如果派生类的 "reference"超出范围但基类引用保留,派生对象是否会发生变化?

mysql - 按日期和条件关联两个表

mysql - 如何将某些列数据从第一个表移动到第二个表。并在单个查询中用不同的数据填充第二个表的其他列

sql - 如何使用 postgresql 对字符串上的自定义排序顺序的查询进行排序?