Monday, March 26, 2012

Validating individual gridview row

I don't know if this is possible or if I'm going about it the wrong way - I have a gridview of products and each row has a template field with a textbox to enter a quantity and I have an 'add to cart' link that selects that row and I run my add to cart code using the gridview's SelectedIndexChanged event.

I want to validate the quantity textbox to ensure that there's a positive integer value in there but my problem is that I would like to validate only the selected row as at the moment if I enter a quantity for product 'A' and click add to cart it validates the whole gridview and all the other product rows display the validation error which I don't want as they are not the select product so it doesn't matter if they have no quantity entered.

I am using page.validate within my SelectedIndexChanged event and only running my add to cart code if it is valid and because not all the rows validate it's not running my code.

Is there any way to validate only the selected row of a gridview and ignore the other rows or am I approaching things the wrong way?

Make use of the ValidationGroup propertyhttp://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.validationgroup.aspx

You need to program the LinkButton that selects the row with a value for this property that is unique for each row and the textboxes that are on each row to include the same unique validationgroup name


Thanks Phillip!

No comments:

Post a Comment