ASP.net动态下拉验证

时间:2015-04-21 23:23:15

标签: c# asp.net validation webforms

这似乎是一个相当普遍的问题,但同样常见的解决方案似乎不起作用。基本上,下拉菜单所需的验证控件不会触发。下拉列表是动态填充的,填充下拉列表的代码显示在页面底部。

<asp:DropDownList ID="Event" runat="server"DataTextField="EventTime" ValidationGroup="DD"
    DataValueField="EventID" SelectMethod="GetEvents" AppendDataBoundItems="true" AutoPostBack="true">
    <asp:ListItem Text="Select..." Value="-1" /></asp:DropDownList>

    <asp:RequiredFieldValidator ID="RequiredFieldEvent" runat="server" ControlToValidate="Event" 
        Display="Dynamic" ValidationGroup="DD" InitialValue="-1" AutoPostback="true"
        ErrorMessage="Please select a time"></asp:RequiredFieldValidator>

代码背后:

   public IQueryable<Event> GetEvents([QueryString("bikeID")] int? bikeId)
    {
        var _db = new WLL.DAL.Context();
        IQueryable<Event> query = _db.Events;
        if (bikeId.HasValue && bikeId > 0)
        {
            query = query.Where(b => b.BikeID == bikeId);
        }
        else
        {
            query = null;
        }
        return query;
    }

1 个答案:

答案 0 :(得分:0)

设置InitialValue =&#34; 0&#34;当-1不起作用时,这对我有用。