我想验证一些文本框和下拉列表控件不要清空,如下面突出显示部分:
我的GridView控件代码如下所示:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EMPLOYEEID"
DataSourceID="SqlDataSource1" ShowFooter="True">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:TemplateField>
<FooterTemplate>
<asp:LinkButton ID="LinkButton1" runat="server">Insert</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMPLOYEEID" SortExpression="EMPLOYEEID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("EMPLOYEEID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("EMPLOYEEID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtInsertEmpID" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvInsertEmpID" ControlToValidate="txtInsertEmpID"
Text="*" ForeColor="Red" ValidationGroup="Insert" runat="server" ErrorMessage="EmployeeID is required" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NAME" SortExpression="NAME">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("NAME") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEditName" ControlToValidate="TextBox1" Text="*"
ForeColor="Red" runat="server" ErrorMessage="EmployeeName is required" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("NAME") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtInsertName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvInsertName" ControlToValidate="txtInsertName"
Text="*" ForeColor="Red" ValidationGroup="Insert" runat="server" ErrorMessage="EmployeeName is required" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DEPTID" SortExpression="DEPTID">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" SelectedValue='<%# Bind("DEPTID") %>' runat="server">
<asp:ListItem>Select Department</asp:ListItem>
<asp:ListItem Value="1">SM</asp:ListItem>
<asp:ListItem Value="2">CDS</asp:ListItem>
<asp:ListItem Value="3">AM</asp:ListItem>
<asp:ListItem Value="4">FS</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvEditDept" ControlToValidate="DropDownList1" Text="*"
ForeColor="Red" runat="server" ErrorMessage="Department is required" InitialValue="Select Department" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("DEPTID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlInsertDeptID" runat="server">
<asp:ListItem>Select Department</asp:ListItem>
<asp:ListItem Value="1">SM</asp:ListItem>
<asp:ListItem Value="2">CDS</asp:ListItem>
<asp:ListItem Value="3">AM</asp:ListItem>
<asp:ListItem Value="4">FS</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvInsertDept" ControlToValidate="ddlInsertDeptID"
Text="*" ForeColor="Red" ValidationGroup="Insert" runat="server" ErrorMessage="Department is required"
InitialValue="Select Department" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="Insert"
ForeColor="Blue" />
<asp:ValidationSummary ID="ValidationSummary2" runat="server" ForeColor="Red" />
我不确定是什么问题所以当我点击插入链接按钮时,即使我没有在文本框的底部输入任何内容,也提交了没有任何错误消息的页面。
任何人都可以帮助我吗?
答案 0 :(得分:1)
请删除
,使用以下代码填写必填字段InitialValue =&#34;选择部门&#34; 和错误消息=&#34;需要部门&#34;
<asp:RequiredFieldValidator ID="rfvInsertDept" ControlToValidate="ddlInsertDeptID" Text="*" ForeColor="Red" ValidationGroup="Insert" runat="server">Department is required</asp:RequiredFieldValidator>
答案 1 :(得分:1)
您只是在插入LinkButton中缺少验证组。
<asp:LinkButton ID="LinkButton1" runat="server" ValidationGroup="Insert">Insert</asp:LinkButton>