Search code examples
asp.netvalidationgridviewtemplatefield

validate a template text box with database value


I'm developing a website and have a textbox in gridview template field. This textbox collects the input for the "order quantity" which has to be compared against the "available quantity"(stored in the database).

I use RowEditing, RowUpdating and RowCanceling events for edit operations in gridview.

I tried implementing the above requirement with "TextChanged" Event of text box. But the issue is, when the user edits the quantity and clicks on "Update" button, text box event fires first and update button event doesn't fire. So, user is forced to click the "Update" button again.

Is there any work around for the above scenario or any other event used to implement above requirement?

Thanks, Balaji g


Solution

  • You can Use RangeValidator For That...

        <asp:RangeValidator ID="RangeValidator1" ControlToValidate="YourTexBoxId" 
    runat="server" MinimumValue="0" Type="Integer"
    MaximumValue='<%# GetQuantity(Eval(TotalQaunt).ToString(),Eval(ItemId).ToString()) %>'
     ErrorMessage="RangeValidator"></asp:RangeValidator>
    

    write a function lyk this ON CODE BEHIND

    string GetQuantity(string totalquantity,string itemid)
    {
    DO OPERATRION TO GET QUANTITY.....
    return quantity;
    }