RequiredFieldValidation fire但网格视图的取消按钮不起作用asp.net C#

时间:2017-03-25 11:53:24

标签: c# asp.net gridview requiredfieldvalidator

您好我正在使用带有编辑,删除,更新,取消链接按钮的网格视图。它需要字段验证,当我尝试更新没有任何值的行并在验证摘要中显示错误消息时它会触发。 这里我要取消更新。但它不起作用。请提前帮助谢谢
我有添加HTML代码和图像

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True" DataKeyNames="empid" OnRowCancelingEdit="GridView1_RowCancelingEdit"
         OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting"
         EmptyDataText="NO RECORD FOUND">
        <Columns>
            <asp:TemplateField HeaderText="Employee ID">
                <EditItemTemplate>
                    <asp:TextBox ID="txtempid" runat="server" Text='<%#Eval("empid") %>' ReadOnly="true"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblempid" runat="server" Text='<%#Eval("empid") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Employee Name">
                <EditItemTemplate>
                    <asp:TextBox ID="txtname" runat="server" Text='<%#Eval("name") %>' ></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvtxtname" runat="server" ErrorMessage="Name is required" ControlToValidate="txtname" Text="*" ForeColor="Red"
                        ValidationGroup="Etemplate"  >

                    </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblname" runat="server"  Text='<%#Eval("name") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtfname" runat="server" ValidationGroup="ft" ></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvtxtinsertname" runat="server" ErrorMessage="Name is required for insert" ControlToValidate="txtfname" Text="*" ForeColor="Red"
                         ValidationGroup="ft" >

                    </asp:RequiredFieldValidator>

                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Employee Job">
                <EditItemTemplate>
                    <asp:TextBox ID="txtjob" runat="server"  Text='<%#Eval("job") %>' ></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvjob" runat="server" ErrorMessage="job required" ControlToValidate="txtjob" Text="*" ForeColor="Red"
                        ValidationGroup="Etemplate">

                    </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="txtjob" runat="server" Text='<%#Eval("job") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtfjob" runat="server" ></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvtxtinsertjob" runat="server" ErrorMessage="job is required for insert" ControlToValidate="txtfjob" Text="*" ForeColor="Red"
                         ValidationGroup="ft" > </asp:RequiredFieldValidator>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Department Name">
                <EditItemTemplate>
                    <asp:TextBox ID="txtdeptname" runat="server"  Text='<%#Eval("deptname") %>'></asp:TextBox>
                     <asp:RequiredFieldValidator ID="rfvdeptname" runat="server" ErrorMessage="dept name required" ControlToValidate="txtdeptname" Text="*" ForeColor="Red"
                         ValidationGroup="Etemplate">

                     </asp:RequiredFieldValidator >
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lbldept" runat="server" Text='<%#Eval("deptname") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtfdeptname" runat="server" ></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvtxtinsertdeptname" runat="server" ErrorMessage="deptname is required for insert" ControlToValidate="txtfdeptname" Text="*" ForeColor="Red"
                         ValidationGroup="ft">
                        </asp:RequiredFieldValidator>


                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="operations ">
                <EditItemTemplate>
                   <asp:LinkButton ID="btnupdate" runat="server" Text="Update" CommandName="Update"></asp:LinkButton>
                      <asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel"></asp:LinkButton>
                </EditItemTemplate>
                <ItemTemplate>
                      <asp:LinkButton ID="btnEdit" runat="server" Text="Edit" CommandName="Edit"></asp:LinkButton>
                      <asp:LinkButton ID="btnDelete" runat="server" Text="Delete" CommandName="Delete"></asp:LinkButton>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Button ID="btninsert" runat="server" CommandName="Insert" Text="Insert" ValidationGroup="ft" />
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:ValidationSummary ID="ValidationSummary1" runat="server"  ForeColor="Red" />

Grid view image

1 个答案:

答案 0 :(得分:1)

我得到了答案原因验证错误

 <asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel"   CausesValidation="false"></asp:LinkButton>