You are being redirected to the new blog version. If your browser does not automatically refresh, please click here

[Serializable] DB.NET

.NET development and more (or less)

/// <summary>
/// Daniel Balla's Weblog - Various posts 
/// on .NET Development, ASP.NET, Agile, 
/// TDD, Architecture, SQL and others.
/// </summary>
[Serializable]
class Blog<T> where T : ASP.NET, Ajax, WCF, WF, new()
{
}

Friday, March 23, 2007

Row level validation in a gridview

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") %)' /)

The parenthesis ( ) should be replaced with less-greater than signs < >, they are just a workaround the poor blogger’s editor.

Download sample

Labels:

2 Comments:

Post a Comment

<< Home


Gixen - Free Auction Sniper