如何在ASP.NET(ASPX)的网格视图中的同一文本框中单独生成2个不同的验证消息?

问题描述 投票:0回答:1

我为gridview中的每个文本框添加了多个验证错误消息。

这是我用过的代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" HorizontalAlign="Center" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound">
<Columns>

<asp:TemplateField HeaderText="Price" >
<ItemTemplate>
<asp:TextBox ID ="TextBox3" runat="server" Width="80px" DataField="Product_Price" Text='<%#string.Format("{0:0.00}",Eval("Product_Price"))%>'/>
<asp:Label ID="Label4" Text="AUD" runat="server"></asp:Label>
<asp:Button ID ="Button11" runat="server" OnClick="Price_Update_Click" ValidationGroup="UpdatePrice" CommandArgument="Button11" CommandName="Update"  Text="Update" />
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox3" ErrorMessage="Must be greater than 0.09" Operator="GreaterThan" Type="Currency" ValueToCompare="0.09" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Numbers with only 2 digits after decimal" ControlToValidate="TextBox3" ValidationExpression="^\d{1,9}\.\d{1,2}$"></asp:RegularExpressionValidator>          
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Quantity" >
<ItemTemplate>
<asp:TextBox ID ="TextBox4" runat="server" Width="60px" DataField="Product_Quantity" Text='<%#Eval("Product_Quantity")%>' />
<asp:Button ID ="Button12" runat="server" OnClick="Quantity_Update_Click" ValidationGroup="UpdateQuantity" CommandArgument="Button12" CommandName="Update"  Text="Update" />
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="TextBox4" ErrorMessage="Must be greater than 0" Operator="GreaterThan" Type="Integer" ValueToCompare="0" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Numbers only but no decimals" ControlToValidate="TextBox4" ValidationExpression="^[0-9]*$"></asp:RegularExpressionValidator>      
</ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>

为了测试TextBox3,这些是我测试过的值:

场景1:我输入0.00,我收到以下错误消息:“必须大于0.09”

场景2:我输入0.08,我收到以下错误消息:“必须大于0.09”

场景3:我输入0.09,我收到以下错误消息:“必须大于0.09”

场景4:我输入1,我收到以下错误消息:“小数后只有2位数的数字”

场景5:我输入250,我收到以下错误消息:“小数后只有2位数的数字”

场景6:我输入1.555,同时收到2条错误消息:“必须大于0.09”“小数后只有2位数的数字。”

场景7:我输入abcd,同时收到2条错误消息:“必须大于0.09”“小数后只有2位数的数字。”

因此,对于TextBox3,基于我为场景6和场景7输入的内容。我想只得到一条错误消息,即“小数点后只有2位数的数字”。

为了测试TextBox4,这些是我测试过的值:

场景1:我输入0,我收到以下错误消息:“必须大于0”

场景2:我输入0.5,同时收到2条错误消息:“必须大于0”“仅数字但没有小数”

场景3:我输入10.5,同时收到2条错误消息:“必须大于0”“仅数字但没有小数”

场景4:我输入abcd,我同时收到2条错误消息:“必须大于0”“仅数字但没有小数”

因此,对于TextBox4,基于我为场景2,场景3和场景4输入的内容。我想只获得一条错误消息,即“仅数字但没有小数”。

因此,对于TextBox3,我只需要修复场景6和场景7。

另一方面,对于TextBox4,我只需要修复场景2,场景3和场景4

如果我的.aspx代码中存在任何小错误,那么如果提供推荐的语法解决方案会有所帮助。

asp.net aspxgridview
1个回答
0
投票

您可以在所有验证器“Display = Dynamic”中使用该属性并尝试使用它

© www.soinside.com 2019 - 2024. All rights reserved.