Wednesday, March 28, 2012

Validate Only Certain GridView Row

We've got a GridView control which displays various information and has one column with a TextBox and another with a Button that adds a new record to the database. This works fine, it gets the value from the TextBox and the other controls perfectly and there is no problems inserting them into the database. The problem is, however, trying to validate the TextBox input, as it has to be an integer.

Using a RequiredFieldValidator (to make sure it's not null) and a RegularExpressionValidator (to make sure it's an integer) also works perfectly, but only when there's one record in the GridView. When there's more than one, every TextBox is validated, but we only want the one on the same row as the button that was clicked to be validated.

In other words, if the button on row #1 is clicked, only the TextBox on row #1 should be validated, if the button on row #2 is clicked, only the TextBox on row #2 should be validated and so on.

The index of the row can be found by sender.Parent.Parent.RowIndex (sender being the button) but we're unsure of how to make only that row be validated.

Can anybody help?

Thanks.

Why do you use a CustomValidator control?
Why don't you use a CustomValidator control?

Could you give me a bit of a clue as to how to write one for this situation?

This being my first real project, the concepts are relatively easy to figure out but putting them into practice is alot harder.


Use this link. Basically you create a customvalidator control in place of your requiredfieldvalidator and regularexpressionvalidator.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolscustomvalidatormemberstopic.asp


The validation part itself is fine, it now works exactly the same as the RequiredFieldValidator and the RegularExpressionValidator, but the problem still remains, it validates every TextBox, not just the one from the specific row.
please post your code.

Just took out the expression part for the sake of simplicity, but it makes no difference to whether it validates the right control or not.

The "sender.Parent.Parent.RowIndex" bit is supposed to select only the correct row, but obviously that doesn't work, cause it does every one.

Protected Sub Quantity(ByVal senderAs Object,ByVal argsAs ServerValidateEventArgs)Dim QuantityAs TextBox = ProductsGrid.Rows(sender.Parent.Parent.RowIndex).FindControl("Quantity")If Quantity.Text =""Then args.IsValid =False Else args.IsValid =True End IfEnd Sub

Hi,b00f;

You may use client side validationg with Javascript in your page.

Here is the sample of a client side validationg of RadioButton Wish it helps;

http://www.codeproject.com/aspnet/ClientsideValidationRB.asp


rexlin:

Hi,b00f;

You may use client side validationg with Javascript in your page.

Here is the sample of a client side validationg of RadioButton Wish it helps;

http://www.codeproject.com/aspnet/ClientsideValidationRB.asp


Hi,b00f;

You may use client side validationg with Javascript in your page.

Here is the sample of a client side validationg of RadioButton Wish it helps;

http://www.codeproject.com/aspnet/ClientsideValidationRB.asp

Cheers for the suggestion, but we just managed to figure it out in an easier way (perhaps not the best way, good enough though).

Simply, when the button is clicked, it goes through each row in the GridView and sets the validators as being true (irrespective of what is in the TextBox). Then, it calls the validate method on only the controls on the same row from which the button was clicked (using sender.Parent.Parent.RowIndex). The code is below, it's probably easier to understand then my explanation.

Dim RowAs GridViewRowFor Each RowIn ProductsGrid.RowsDim RequiredQuantityTrueAs RequiredFieldValidator = ProductsGrid.Rows(Row.RowIndex).FindControl("RequiredQuantity")Dim QuantityExpressionTrueAs RegularExpressionValidator = ProductsGrid.Rows(Row.RowIndex).FindControl("QuantityExpression") RequiredQuantityTrue.IsValid =True QuantityExpressionTrue.IsValid =TrueNext RowDim RequiredQuantityAs RequiredFieldValidator = ProductsGrid.Rows(sender.Parent.Parent.RowIndex).FindControl("RequiredQuantity")Dim QuantityExpressionAs RegularExpressionValidator = ProductsGrid.Rows(sender.Parent.Parent.RowIndex).FindControl("QuantityExpression")RequiredQuantity.Validate()QuantityExpression.Validate()If RequiredQuantity.IsValidAnd QuantityExpression.IsValidThen
...

No comments:

Post a Comment