ASP.NET Webforms的整数下拉列表

时间:2014-07-14 11:51:35

标签: c# asp.net webforms visual-studio-2013

我有一个带有模型类的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个整数的下拉列表?

1 个答案:

答案 0 :(得分:0)

我必须通过将其添加到后面的代码来解决它:

 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>