Link to home
Start Free TrialLog in
Avatar of zameer21
zameer21

asked on

Gridview item template text box validation

Asp.net Gridview:I have Gridview being pupulated by Product and Unit Price from database and i also have item templelate that has a text box for quanity and another item template for button which says "Add to cart".Now i want to add validation to the button,that it checks that quanity textbox is required and also a integer.How can i do it at a gridview row level
Avatar of valkyrie_nc
valkyrie_nc
Flag of United States of America image

Add RequiredFieldValidator and RegularExpressionValidator (or a custom validator that does both) to the Quantity textbox, and make sure the button on your gridview row has the attribute CausesValidation set to true.  That should do it.

Just for fun, here's a good review of ASP.Net validators:  http://www.codeproject.com/aspnet/aspnetvalidation.asp


hth

valkyrie_nc
Avatar of zameer21
zameer21

ASKER

I tried that but that does validation to whole gridview but not to the row on which the button was pressed.
ASKER CERTIFIED SOLUTION
Avatar of valkyrie_nc
valkyrie_nc
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
here is what i would do

protected void dg_ItemDataBound()
{

      if (e.row.rowtype == rowtype.datarow)
{

// reference the controls
RequiredFieldValidtor reqv = (RequiredFieldValididator)e.row.findcontrol("reqV");
.. any other validators you may need ...
TextBox unitprice = (textbox)e.row.findcontrol("txtb");

reqv.controltovalidate = unitprice.id;


{

an approach like this will work, i didnt build this in vs so the syntax is a little off, but you need to hook this event handler up to the gridview.ItemDataBound or RowDataBound i believe.

Any questions let me know
Thats was cool Valkyrie,I was not able to verify Rodmjay's answer.