C# 验证程序不阻止回发
我在内容页中有一个GridView。在GridView中,我可以执行删除和插入操作。对于insert,我需要确保StudentNo和Amount的输入不能为空。所以我使用了RequiredFieldValidator。问题是,当我单击insert时,即使这两个文本框是空的,它仍然会回发。在我的服务器端函数GridView_row命令中,我总是必须检查是否(Page.IsValid)。我的问题是,为什么即使验证失败,RFV也不会在客户端停止。下面是我的aspx代码,有人能帮我看看我遗漏了什么吗C# 验证程序不阻止回发,c#,asp.net,validation,gridview,C#,Asp.net,Validation,Gridview,我在内容页中有一个GridView。在GridView中,我可以执行删除和插入操作。对于insert,我需要确保StudentNo和Amount的输入不能为空。所以我使用了RequiredFieldValidator。问题是,当我单击insert时,即使这两个文本框是空的,它仍然会回发。在我的服务器端函数GridView_row命令中,我总是必须检查是否(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行命令事件中截获的回发。您需要拦截单击并调用事件。stopPropagation()
或设置事件。cancelBubble=true
,具体取决于您的浏览器:
void some_js_onclick_function_called_within_a_gridview( event ) {
if ( event.stopPropagation )
event.stopPropagation();
else
event.cancelBubble = true;
}
但是,您必须应对验证,这很容易,因为ASP.NET有一个用于验证的客户端API:
最简单的答案是从GridView页脚中删除输入字段,牺牲“外观”来处理验证,而不让GridView搞砸
或者,将按钮保留在页脚,但将LinkButton服务器控件转换为基本html
控件,并将onclick
属性设置为上述某些验证函数,并在成功进行客户端验证时调用\doPostBack('SomeEventTargetName','SomeEventTargetArgument')
。始终检查服务器端的输入以及安全措施
有关ASP客户端验证API的更完整的概念,请查看已发布项目的obj目录中的WebUIValidation.js
i.e.: obj\Release\Package\PackageTmp\Scripts\WebForms\WebUIValidation.js
也许这篇文章的解决方案会对你有所帮助:@ConnorsFan:那篇文章的解决方案是什么?“这和另一个有什么关系?”TimSchmelter,这是Hans Ajani对自己帖子的回答。他应该接受它,因为现在人们经常提到它。我将就此给他留下评论。附录:Hans的帖子中也忽略了验证器(回发发生在不应该发生的时候),所以我觉得这和GLP的情况类似。只是尝试过了。仍在发回。