Validate Controls Inside GridView
By AzamSharp
Views: 12117

Introduction:

In this article I will show you that how you can validate the controls which are inside the GridView control. For this I will make the editable GridView and use the RequiredFieldValidator for the TextBoxes. I will also show you that how you can use validation against the DropDownList control which is contained inside the GridView control.

Populating the GridView Control:

Our first task is to populate the GridView control. Let's first see the HTML code of the GridView control so that you will have the idea of what the GridView looks like:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDataBound="GridView1_RowDataBound" Font-Names="Verdana" Font-Size="Small">

<Columns>

<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />

<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />

<asp:TemplateField HeaderText="Description">

<ItemTemplate>

<asp:Label ID="Label1" Text='<%# Eval("Description") %>' runat="server" Font-Names="Verdana"></asp:Label>&nbsp;<br />

<br />

</ItemTemplate>

<EditItemTemplate>

<asp:RequiredFieldValidator ErrorMessage="This field cannot be blank" ControlToValidate="TextBox1" ID="RequiredFieldValidator1" runat="server"></asp:RequiredFieldValidator>

<asp:TextBox ID="TextBox1" Width="98%" Text='<%# Eval("Description") %>' runat="server"></asp:TextBox>

</EditItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Choose">

<ItemTemplate>

<asp:DropDownList ID="DropDownList1" DataSource='<%# PopulateDropDownList() %>' DataTextField="CategoryName" DataValueField="CategoryName" runat="server" Width="116px">

</asp:DropDownList><br />

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="DropDownList1"

ErrorMessage="Please Select the Item" InitialValue="Select an item"></asp:RequiredFieldValidator>

</ItemTemplate>

</asp:TemplateField>

<asp:CommandField ShowEditButton="True" />

</Columns>

</asp:GridView>

 

As you can see that two BoundColumns and two TemplateColumns. The CategoryID and Category Name are the Bound columns which are bound to the "CategoryID" and "CategoryName" fields in the Categories table in Northwind Database. The "Description" and "Choose" columns are template columns. The GridView looks something like this:

If you carefully read the HTML code of the GridView which I have posted above then you will notice one very special thing. The RequiredFieldValidator for the TextBox control in the Description column is placed in the EditItemTemplate of the GridView control. This is because the TextBox is in the EditItemTemplate and that is what we need to validate. If you put the RequiredFieldValidator in any ItemTemplate or any other Template supported by GridView you will recieve an error saying that GridView is unable to find the control to validate.

I have done the same thing with the DropDownList. Since the DropDownList is in the ItemTemplate of the GridView control so I placed the RequiredFieldValidator inside the ItemTemplate right beneath the DropDownList control.

It is a common scenario that you want the first item of the DropDownList to be some message saying "Please select an item". For this you can add the item dynamically inside the GridView_RowDataBound event.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

DropDownList ddList = (DropDownList)e.Row.FindControl("DropDownList1");

ddList.Items[0].Text = "Select an item";

ddList.Items[0].Value = "Select an item";

}

}

Don't forget to assign the value property of the DropDownList to "Select an item" or any other text you like. It is this value that is compared against a RequiredFieldValidator. Now, you can easily set the RequiredFieldValidator's InitialValue property to "Select an item" or whatever your text was which means that you cannot leave the control whose value or text property is "Select an item" or the text set by you.

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="DropDownList1"

ErrorMessage="Select the Item" InitialValue="Select an item"></asp:RequiredFieldValidator>

You will also notice that when you loose focus from the control the validation control event will be fired. This is because the validation controls have a property "EnableClientScript" which means that the validation will be fired on the client side first and then on the server side. If you set EnableClientScript = false then client validation won't be fired and the validation control will wait for Page.Validate event to occur. If Page.Validate does not occur then no validation will be fired.

It is a good idea to keep EnableClientScript enabled so validation can be fired both on the client side as well as the server side.

I have attached the source code for this project. Please feel free to download it.

I hope you liked this article, happy coding!

If you are one of the thousands that visit GridViewGuy for your .NET articles and resources, you might be interested in making a donation. Extra cash helps pay for the hosting services and speed things up around here, and makes this website possible.

Make a Donation

Once, again thank you very much and remember its because of you FINE people that this website is up and running.

 

Export Button is a custom control that let's you export your DataGrid or TextBox data to several different formats. The control is extremely easy to use and also exposes design time features. In this article I will discuss some of the features of the Export Button and how it benefits the developer.

BUY IT NOW

 

 

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: Thanks buddy.
Name: Ramesh
Date: 3/27/2007 11:18:10 PM
Comment:
You people are great, this is my true acknoldgement
Subject: hi
Name: krishna
Date: 6/13/2007 4:19:09 AM
Comment:
its very helpful

..........
Thanks a Lot
Subject: great
Name: Ira
Date: 9/7/2007 7:48:13 AM
Comment:
I have seen Azam's articles on various websites. Brillinat work each time. very simple, clear and precise. thanks.
Subject: RE: great
Name: AzamSharp
Date: 9/9/2007 9:28:04 AM
Comment:
Hi Ira,

Thanks for the kind words.
Subject: ok
Name: hongliu
Date: 11/29/2007 7:57:16 PM
Comment:
Great Help!
Subject: Hiding non-key column in editable gridview fails editing
Name: Emad
Date: 2/1/2008 1:38:40 AM
Comment:
I'm populating the gridview of city based on the country selected from the dropdownlist. I have the edit and delete link in each row. I've handled the issue of making primary key column invisible by using datakey property, but how do I hide the Country code column which is foreign key. The edit feature works only when it is visible but stops working when I set it's visible property to false.
Plz help me with this issue.
Subject: RE: Hiding non key column
Name: AzamSharp
Date: 2/4/2008 8:19:27 AM
Comment:
Hi Emad, I am not sure if I understand your problem clearly.
Subject: Great
Name: srikrishna
Date: 2/11/2008 12:02:06 AM
Comment:
You are simply great people.....amazing site......
Subject: Thanks
Name: Raj
Date: 2/14/2008 1:57:21 AM
Comment:
You are simply great...
Lot of thanks to you...
Subject: RequiredFieldValidator inside GridView
Name: Marty Spallone
Date: 2/18/2008 8:11:22 PM
Comment:
Nice article. How do you prevent duplicate ErrorMessage(s) when you add a required field validator inside a GridView with a validation summary on a form ? Our users complain that they get as many errors as data rows and I want to show the asterisk (*) on each row that is in error but only one error message (no duplicates).

Would love help on this!
thanks



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008