jat465
asked on
Range validator not preventing row updating operation in gridview
I have a range validator for a textbox in a gridview template field. It checks that the textbox has an integer in specfic range. In the associated gridview_rowupdating code I have an "if" statement based on "page.isvalid". The code inside the if statement is being executed even though the page is not valid due to not having an appropriate integer in the textbox.
<asp:GridView ID="gvGames" runat="server"
AllowSorting ="True"
AutoGenerateColumns="False"
AutoGenerateEditButton="True"
OnRowEditing="gvGames_RowEditing"
OnRowCancelingEdit="gvGames_RowCancelingEdit"
OnRowUpdating="gvGames_RowUpdating"
OnPageIndexChanging="gvGames_PageIndexChanging"
CellPadding="4"
GridLines="None" Width="728px" style="font-size: medium" >
<Columns>
<asp:CommandField ShowDeleteButton="True" ValidationGroup="ValidScore" />
<asp:TemplateField HeaderText="Date" SortExpression="Date">
<EditItemTemplate>
<asp:Label ID="Labe21" runat="server" Text='<%# Bind("Date", "{0:MMM d, yyyy (ddd)}") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label22" runat="server" Text='<%# Bind("Date", "{0:MMM d, yyyy (ddd)}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="VisitingTeam" SortExpression="VisitingTeam">
<EditItemTemplate>
<asp:Label ID="Labe23" runat="server" Text='<%# Bind("VisitingTeam") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label24" runat="server" Text='<%# Bind("VisitingTeam") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="tbVisScore" runat="server" Height="20px"
Text='<%# Bind("VisitingTeamScore") %>' Width="46px"></asp:TextBox>
<br />
<asp:RangeValidator ID="rvalVisScore" runat="server"
ControlToValidate="tbVisScore" Display="Dynamic"
ErrorMessage="Must be a positive integer between 0 and 100" ForeColor="#993300" MinimumValue="0"
Type="Integer" MaximumValue="100" ValidationGroup="ValidScore"></asp:RangeValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("VisitingTeamScore") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="HomeTeam" SortExpression="HomeTeam">
<EditItemTemplate>
<asp:Label ID="Labe25" runat="server" Text='<%# Bind("HomeTeam") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label26" runat="server" Text='<%# Bind("HomeTeam") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="tbHomScore" runat="server" Height="20px"
Text='<%# Bind("HomeTeamScore") %>' Width="46px"></asp:TextBox>
<br />
<asp:RangeValidator ID="rvalHomScore" runat="server"
ControlToValidate="tbHomScore" Display="Dynamic"
ErrorMessage="Must be a positive integer between 0 and 100" ForeColor="#993300"
MaximumValue="100" MinimumValue="0" Type="Integer"
ValidationGroup="ValidScore"></asp:RangeValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("HomeTeamScore") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Result" SortExpression="Result" Visible="False">
<EditItemTemplate>
<asp:SqlDataSource ID="ResultCatsDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:SQL2005_503839_gamevidenceConnectionString2 %>"
SelectCommand="SELECT DISTINCT ResultCatsID, ResultText FROM ResultCats ORDER BY ResultCatsID"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ResultCatsDataSource"
DataTextField="ResultText" DataValueField="ResultText" SelectedValue='<%# Bind("Result") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label27" runat="server" Text='<%# Eval("Result") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle ForeColor="#333333" HorizontalAlign="Center" BackColor="#FFCC66" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Protected Sub gvGames_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
'Retrieve the table from the session object.
If Page.IsValid Then
< calculations >
End If
End Sub
You've set a validation group for the validator but the update button won't know to use that validationgroup, hence page.isvalid returns true.
ASKER
How do I tell the update button to use that validationgroup?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.