Click here to Skip to main content
15,887,978 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys,

-There is 2 columns(i.e txtAQty(Available Quantity) and txtQtyOff(Quantity Needed)) and 2 different text boxes in a GridView Footer Row.
-When I insert the data using Footer Row it save the data in DataRow.
-Now I want to compare values of the two text boxes(i.e txtAQty(Available Quantity) and txtQtyOff(Quantity Needed))
-I implement CustomValidator which is not working properly.
-When I insert a record it is supposed to show the error message.
-But it is inserting the record as well and displaying the error message also..

-Please Help..

Thanks in Advance.


Behind The Code:
****************
C#
protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs args)
       {

           CustomValidator cv = (CustomValidator)sender;

           GridViewRow grv1 = (GridViewRow)cv.NamingContainer;

           //dim gvr As GridViewRow = cv.NamingContainer

           decimal txtofstock = 0;
           decimal txtastock = 0;

           decimal.TryParse(((TextBox)GridView1.FooterRow.FindControl("txtQtyOff")).Text, out txtofstock);
           decimal.TryParse(((TextBox)GridView1.FooterRow.FindControl("txtAQty")).Text, out txtastock);

           if (txtofstock > txtastock)
           {
               Response.Write("Error");
               //lblError.Text = "Error ";
           }

       }


ASPX Page:
**********
XML
<Columns>
                <asp:TemplateField HeaderText="AvailableQuantity" HeaderStyle-ForeColor="Maroon" HeaderStyle-BackColor="LightGray">
                    <ItemTemplate>
                        <asp:Textbox ID = "txtAQty"   runat ="server" Text='<%# Eval("AQty") %>'  BorderWidth="0" Width="100%"  ></asp:Textbox>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID ="txtAQty" runat="server"  AutoPostBack="true" ></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
            <Columns>
                <asp:TemplateField HeaderText="QuantityOffered"  HeaderStyle-ForeColor="Maroon" HeaderStyle-BackColor="LightGray" >
                    <ItemTemplate>
                        <asp:TextBox ID="txtQtyOff" runat="server" Text='<%# Eval("QtyOff") %>' BorderWidth="0" Width="100%"></asp:TextBox>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtQtyOff" runat="server" AutoPostBack="true"   ></asp:TextBox>
                        <asp:CustomValidator  ID="CmpVal" Text = "*"  OnServerValidate="CustomValidator1_ServerValidate" runat="server" ValidateEmptyText="true" ControlToValidate="txtAQty" ></asp:CustomValidator>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
Posted
Updated 26-Aug-13 19:17pm
v2

1 solution

There are two things.
- First in CustomValidator1_ServerValidate method, set the
args.IsValid = false;
if validation fails. This indicates whether the validation is failed or not. You are just showing error but not making page status invalid.

- Second, add additional check for
Page.IsValid
before inserting record in DB. It checks whether all the validation on the page is passed or not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900