由于某种原因,RequiredFieldValidation不适用于asp:DetailsView:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
CellPadding="4" DataKeyNames="Id" DataSourceID="EntityDataSource1"
DefaultMode="Insert" ForeColor="#333333" GridLines="None"
Height="50px" Width="125px" OnItemInserted="DetailsView1_ItemInserted">
<AlternatingRowStyle BackColor="White" />
<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
<EditRowStyle BackColor="#2461BF" />
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
<Fields>
<asp:BoundField DataField="Id" InsertVisible="false" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="First_Name" HeaderText="Name" SortExpression="First_Name" />
<asp:BoundField DataField="LastName" InsertVisible="false" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
<asp:TemplateField HeaderText="Salary" SortExpression="Salary">
<EditItemTemplate>
<asp:TextBox ID="salaryTB" runat="server" Text='<%# Bind("Salary") %>' ValidationGroup="DetailsViewValidation"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvSalary" runat="server" ControlToValidate="salaryTB"
InitialValue="0" Display="Dynamic" ErrorMessage="ss" SetFocusOnError="true" ValidationGroup="DetailsViewValidation"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="JobTitle" InsertVisible="false" HeaderText="JobTitle" SortExpression="JobTitle" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
</asp:DetailsView>
我尝试了所有我能想到的东西,当然,我在网上搜索过它。 当我单击Insert时,只需插入值(0),不会出现任何问题。