Question

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

Was it helpful?

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;
}

OTHER TIPS

I think I'd try the CustomValidator. With the CustomValidator, you can define the validation code in the "_ServerValidate" event handler.

In your validation code, you can call the database to get the current value and then compare it to the submitted value (which would be available via the event handler's ServerValidateEventArgs paramenter if you specify a ControlToValidate).

In order to look up the current value in the database, you'll need to know which record was clicked. For that, you could either use the row button's CommandArgument property or include a hidden field in your row to store some type of ID. Then you could capture that ID and use it to look up the desired record in the database.

Updated with Code

Protected Sub cusCustom_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs)
    Dim cv As CustomValidator = CType(source, CustomValidator)
    Dim HiddenField_ID As HiddenField = CType(cv.Parent.FindControl("HiddenField_ID"), HiddenField)
    *** Use ID to look up value in database ***
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top