c# - 验证器不阻止回发

标签 c# asp.net validation gridview

我在内容页面中有一个 GridView。在 GridView 中,我可以执行删除和插入操作。对于插入,我需要确保 StudentNo 和 Amount 的输入不能为空。所以我使用了 RequiredFieldValidator。问题是当我单击插入时,即使这两个文本框为空,它仍然会回发。在我的服务器端函数 GridView_RowCommand 中,我总是必须检查 if(Page.IsValid)。我的问题是为什么 RFV 即使验证失败也不会在客户端停止。以下是我的 aspx 代码,谁能帮我看看我遗漏了什么?

<asp:Content ID="Content3" ContentPlaceHolderID="ContentMain" Runat="Server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
        ShowFooter="true" AllowPaging="True" PageSize="40" AllowSorting="True" 
        DataKeyNames="ID" onrowcommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lbDelete" CommandArgument='<%# Eval("ID") %>' CommandName="DeleteRow" runat="server" 
                CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this row?')" 
                Font-Underline="True" Font-Bold="True">Delete</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="ID" SortExpression="">
            <ItemTemplate>
                <asp:Label ID="Label_ID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:LinkButton ID="lbInsert" CommandName="InsertRow" ValidationGroup="Insert" runat="server" 
                Font-Underline="True" Font-Bold="True" CausesValidation="true">Insert</asp:LinkButton>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="InstCode" SortExpression="">
            <ItemTemplate>
                <asp:Label ID="Label_InstCode" runat="server" Text='<%# Bind("InstCode") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="StudentNo" SortExpression="">
            <ItemTemplate>
                <asp:Label ID="Label_StudentNo" runat="server" Text='<%# Bind("StudentNo") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtStudentNo" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvStudentNo" runat="server" ErrorMessage="StudentNo is a required field" 
                    ControlToValidate="txtStudentNo" Text="*" ForeColor="Red" ValidationGroup="Insert">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="AccountType" SortExpression="">
            <ItemTemplate>
                <asp:Label ID="Label_AccountType" runat="server" Text='<%# Bind("AccountType") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Amount" SortExpression="">
            <ItemTemplate>
                <asp:Label ID="Label_Amount" runat="server" Text='<%# Bind("Amount") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtAmount" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvAmount" runat="server" ErrorMessage="Amount is a required field" 
                    ControlToValidate="txtAmount" Text="*" ForeColor="Red" ValidationGroup="Insert">
                </asp:RequiredFieldValidator>
        </FooterTemplate>
        </asp:TemplateField>
    </Columns>
    </asp:GridView>
    <asp:ValidationSummary ID="ValidationSummary1" ValidationGroup="Insert" ForeColor="Red" runat="server" />
    <br /><br />
    <asp:Label ID="lblMessage" runat="server" Text="" ForeColor="Red"></asp:Label>
</asp:Content>

最佳答案

我将我的评论移到这里,因为还有更多需要考虑

GridView 中的验证,尤其是当您绕过 GridView 的编辑模式时,会由于事件冒泡而导致回发。

服务器控件单击事件在 Gridview 中冒泡并触发可在 GridView RowCommand 事件中拦截的回发。您需要拦截点击并调用 event.stopPropagation()set event.cancelBubble = true取决于您的浏览器:

void some_js_onclick_function_called_within_a_gridview( event ) {
  if ( event.stopPropagation )
    event.stopPropagation();
  else
    event.cancelBubble = true;
}

但是您必须应对验证,这很容易,因为 ASP.NET 有一个用于验证的客户端 API:ASP.NET Validation in Depth

最简单的答案是从 GridView 页脚中删除输入字段,牺牲“外观和感觉”来处理验证,而不会让 Gridview 搞砸。

或者,将按钮保留在页脚中,但将 LinkBut​​ton 服务器控件转换为基本的 html <button>控制并设置 onclick将某些验证函数归为上述属性并调用 __doPostBack('SomeEventTargetName', 'SomeEventTargetArgument')成功的客户端验证。始终检查服务器端的输入以及安全措施。

有关 ASP 客户端验证 API 的更完整概念,请查看您已发布项目的 obj 目录中的 WebUIValidation.js .

i.e.: obj\Release\Package\PackageTmp\Scripts\WebForms\WebUIValidation.js

关于c# - 验证器不阻止回发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36551103/

相关文章:

asp.net - ImageUrl自定义控件设计时间

干净地验证输入

c# - Twitter API + OAuth : Can't send status updates, 获取 401

c# - 如何永久存储cookie

c# - 如何向 MFC 应用程序添加 list 并设置支持的操作系统?

ASP.NET 在回发期间显示进度条

WPF ValidationRules 禁用 PropertyChanged

c# - ssis 包的 Dynamics Crm 数据验证

c# - 如何在 ASP.NET MVC 中的 View 上只显示 DATE 部分

c# - 有趣的代码片段