ASP.NET验证无法在ASP FormView中运行

时间:2012-03-28 17:38:15

标签: validation formview

ASP.NET验证在ASP:FormView中似乎不起作用。有人能让我知道我做错了什么吗?谢谢。

这是代码。

    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="ValidationSummary1" />
    <asp:FormView ID="fv_LeaveRequest" runat="server" DataKeyNames="REQ_ACTION_ID">
    <InsertItemTemplate>
            <label>Leave Type</label>
            <asp:DropDownList ID="ddl_LeaveType" runat="server">
            </asp:DropDownList>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="This field is required" ValidationGroup="ValidationSummary1" Text="Required" ControlToValidate="ddl_LeaveType"></asp:RequiredFieldValidator>         
        <asp:Button ID="LeaveReqBtn" runat="server" Text="Submit Leave Request" CommandName="CustomInsert"
            CssClass="button-position" ValidationGroup="ValidationSummary1" CausesValidation="true" />
    </InsertItemTemplate>
    <ItemTemplate>            
            <asp:LinkButton ID="Edit" runat="server" Text="Edit / Change Request" CommandName="Edit" CssClass="button-position btn-padding" ValidationGroup="ValidationSummary1" />           
            <label>
                Leave Type</label>
            <asp:DropDownList ID="ddl_LeaveType" runat="server">
            </asp:DropDownList>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="This field is required"
                ValidationGroup="ValidationSummary1" Text="Required" ControlToValidate="ddl_LeaveType"></asp:RequiredFieldValidator>
               </ItemTemplate>
    <EditItemTemplate>           
            <asp:LinkButton ID="Cancel" Text="Cancel" CommandName="Cancel" runat="server" CssClass="button-position btn-padding"
                ValidationGroup="ValidationSummary1" />
            <label>
                Leave Type</label>
            <asp:DropDownList ID="ddl_LeaveType" runat="server">
            </asp:DropDownList>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="This field is required"
                ValidationGroup="ValidationSummary1" Text="Required" ControlToValidate="ddl_LeaveType"></asp:RequiredFieldValidator>
              </EditItemTemplate>
</asp:FormView>

1 个答案:

答案 0 :(得分:1)

您需要为初始值添加一个属性到RequiredFieldValidators。当您使用RequiredFieldValidator DropDownList时,需要添加InitialValue="0"