我正在尝试实现一个下拉菜单,其中包含数字 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/