ASP的整数下拉列表.净Webforms

本文关键字:Webforms 下拉列表 整数 ASP | 更新日期: 2023-09-27 18:12:43

我有一个带有模型类的Web表单项目:

public class PayrollPeriod
    {
        [ScaffoldColumn(false)]
        [Key]
        public int PayrollPeriodId { get; set; }
        [ScaffoldColumn(false)]
        [Required]
        public string UserId { get; set; }
        public virtual ApplicationUser User { get; set; }
        [Range(1, 24, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
        [Required, Display(Name = "Working Hours In A Day")]
        public int HrsInDay { get; set; }
        //...
     }
脚手架生成如下Insert.aspx:
 <asp:FormView runat="server"
            ItemType="PayMaster.Models.PayrollPeriod" DefaultMode="Insert"
            InsertItemPosition="FirstItem" InsertMethod="InsertItem"
            OnItemCommand="ItemCommand" RenderOuterTable="false">
            <InsertItemTemplate>
                <fieldset class="form-horizontal">
                    <legend>Insert Payroll Period</legend>
                    <asp:ValidationSummary runat="server" CssClass="alert alert-danger" />
                    <asp:DynamicControl Mode="Insert" DataField="StartDate" runat="server" />
                    <asp:DynamicControl Mode="Insert" DataField="EndDate" runat="server" />
                    <asp:DynamicControl Mode="Insert" DataField="HrsInDay" runat="server" />
                    <asp:DynamicControl Mode="Insert" DataField="DaysInWeek" runat="server" />

我不太熟悉ASP Web Forms。如何编辑HrsInDay的动态控件以显示整数1 - 24的下拉列表?

ASP的整数下拉列表.净Webforms

我必须通过在后面的代码中添加以下内容来解决这个问题:

 public Dictionary<int, int> GetHrsInDay()
        {
            Dictionary<int, int> hrsInDay = new Dictionary<int, int>();
            for (int i = 1; i < 25; i++)
            {
                hrsInDay.Add(i, i);
            }
            return hrsInDay;
        }

,然后这个在aspx页面上,并将下拉列表中的SelectedValue绑定到项目HrsInDay:

 <asp:ValidationSummary runat="server" CssClass="alert alert-danger" />
                    <asp:DynamicControl Mode="Insert" DataField="StartDate" runat="server" />
                    <asp:DynamicControl Mode="Insert" DataField="EndDate" runat="server" />
                    <div class="form-group">
                        <asp:Label runat="server" CssClass="col-sm-3 control-label">Working Hours In A Day</asp:Label>
                        <div class="col-sm-4">
                            <asp:DropDownList ID="ddlHrsInDay" runat="server" CssClass="form-control"
                                AutoPostBack="True" 
                                AppendDataBoundItems="true"
                                SelectMethod="GetHrsInDay"
                                DataTextField="Value" DataValueField="Key"
                                SelectedValue="<%# BindItem.HrsInDay%>"
                                OnSelectedIndexChanged="ddls_SelectedIndexChanged">
                                <asp:ListItem Value="0" Text="Select"></asp:ListItem>
                            </asp:DropDownList>
                        </div>
                    </div>