Mar
23
Written by:
Daniel Balla
Fri, 23 Mar 2007 18:57:03 GMT
A couple days ago a friend asked me how can you make the ASP.NET validators validate the contents of a gridview at row level, so that each row works independently. For example, in the grid below use a requiredfieldvalidator on each row so that the Save button would validate the description textbox on the same row, but wouldn’t validate any of the other rows’ descriptions.

In this case, clicking the save button in the “Dairy products” row would cause the validation to suppress the postback, but clicking the save button of any other row wouldn’t.
The solution is simple: use the validation group bound to a unique row identifier. In our example (which displays the contents of the Categories table of the Northwind database) the binding would look like:
<asp:TextBox Width="300px" ID="txtDescription" runat="server" Text='<%# Bind<"Description"> %>' />
<asp:RequiredFieldValidator ID="reqDescription" runat="server" ControlToValidate="txtDescription" ErrorMessage="*" ValidationGroup='<%# Bind<"CategoryID"> %>' />
<asp:Button ID="butSave" runat="server" Text="Save" ValidationGroup='<%# Bind<"CategoryID"> %>' />
Download Sample Code
Tags:
1 comments so far...
Re: Row level validation in a gridview
Very usefull, yet very simple.
By Daniel C. on
Tue, 19 Aug 2008 13:27:54 GMT
|